Python AWS tutorial

First AWS connection, first service and the three ways to reach any AWS API through boto3.

This tutorial walks you through your first AWS integration with Zato - from creating the connection in the Dashboard to invoking any AWS API from a Python service.

Step 1 - Create the connection

Open the Dashboard and go to Cloud → AWS. Click "Create a new connection" and fill in the form:

FieldValue
NameMy AWS
RegionThe region you work in, e.g. us-east-1
Access key IDThe access key ID of your IAM user
Endpoint URLLeave it empty for AWS itself

After the connection is created, click "Change secret access key" next to it and enter the secret access key that pairs with your access key ID.

You can now click "Ping" to confirm that the credentials work - it calls STS GetCallerIdentity, which requires no permissions beyond the credentials themselves.

Step 2 - Write the first service

The connection is available in every service through self.aws, referenced by name. Let's list your S3 buckets:

# -*- coding: utf-8 -*-

# Zato
from zato.server.service import Service

class ListBuckets(Service):

    def handle(self):

        # Get the connection by its Dashboard name
        conn = self.aws['My AWS']

        # Call S3
        response = conn.s3.list_buckets()

        # Extract the names
        names = []
        for bucket in response['Buckets']:
            names.append(bucket['Name'])

        # Return them to the caller
        self.response.payload = {'buckets': names}

Hot-deploy the service and invoke it - the response will be a list of your bucket names:

{"buckets": ["invoices", "reports", "backups"]}

Step 3 - Understand the API

There are three ways to reach an AWS service through the connection, and all of them go through the same underlying boto3 session:

Attribute access - each AWS service is an attribute of the connection. This is the most convenient form and covers the common cases:

conn = self.aws['My AWS']

conn.s3.list_buckets()
conn.sqs.list_queues()
conn.ec2.describe_instances()

Explicit clients - conn.client(name) is the same as boto3.client(name). Use it for services whose names are not valid Python attributes, such as lambda, or when you prefer to be explicit:

conn = self.aws['My AWS']

aws_lambda = conn.client('lambda')
aws_lambda.invoke(FunctionName='process-order')

Resources - conn.resource(name) is the same as boto3.resource(name), the higher-level object-oriented API that some services offer:

conn = self.aws['My AWS']

dynamodb = conn.resource('dynamodb')
table = dynamodb.Table('customers')
table.put_item(Item={'customer_id': 'abc-123', 'name': 'Jane'})

Clients and resources are cached, so accessing conn.s3 repeatedly always returns the same object, and conn.s3 is the same object as conn.client('s3').

Step 4 - Explore the services

With the connection in place, every AWS API is one attribute away. The pages below cover the most common integrations with complete examples:

  • S3 - buckets, objects, presigned URLs
  • EC2 - describe and manage instances
  • SQS - queues, sending and receiving messages
  • SNS - topics, publishing, subscriptions
  • DynamoDB - tables, items, queries
  • Lambda - invoking functions
  • CloudWatch - metrics and logs

More resources

Learn more