0

I would like to manage my AWS Lambda Functions from Python script, without having to use the AWS Website Console. The idea is to be able to quickly recreate/migrate/setup my applications objects (tables, functions etc.) into a new AWS Cloud Account or Region.

It is easy to do this with DynamoDB tables, for instance:

import boto3
resource = boto3.resource(service_name='dynamodb', region_name='region', ...)
resource.create_table(
                TableName='table_name',
                KeySchema=[...],
                AttributeDefinitions={...},
                ProvisionedThroughput={...}
            )

Done! I just created a new DynamoDB table from Python Script. How can I do the same for Lambda Functions? Say... create a new function, configure ‘Function Name’ and ‘Runtime’, maybe set up a ‘Role’ and upload a script from file. That’d be really helpful.

Thanks in advance.

Pepe
  • 39
  • 5

1 Answers1

1

To create lambda function using boto3, you can use create_function.

The AWS docs also provide an example of how to use the create_function:

response = client.create_function(
    Code={
        'S3Bucket': 'my-bucket-1xpuxmplzrlbh',
        'S3Key': 'function.zip',
    },
    Description='Process image objects from Amazon S3.',
    Environment={
        'Variables': {
            'BUCKET': 'my-bucket-1xpuxmplzrlbh',
            'PREFIX': 'inbound',
        },
    },
    FunctionName='my-function',
    Handler='index.handler',
    KMSKeyArn='arn:aws:kms:us-west-2:123456789012:key/b0844d6c-xmpl-4463-97a4-d49f50839966',
    MemorySize=256,
    Publish=True,
    Role='arn:aws:iam::123456789012:role/lambda-role',
    Runtime='nodejs12.x',
    Tags={
        'DEPARTMENT': 'Assets',
    },
    Timeout=15,
    TracingConfig={
        'Mode': 'Active',
    },
)

print(response)
Marcin
  • 215,873
  • 14
  • 235
  • 294
  • Solved. Thank you! – Pepe Jul 30 '20 at 15:03
  • 1
    I put a full example that shows this in context along with some other Boto3 Lambda functions on GitHub, check it out: (aws-doc-sdk-examples)[https://github.com/awsdocs/aws-doc-sdk-examples/blob/91b9e57fbbb2d6b6f8ab94284bf04e8e46d1a247/python/example_code/lambda/boto_client_examples/lambda_basics.py#L114]. – Laren Crawford Aug 04 '20 at 18:14
  • @LarenCrawford Thanks for letting me know :-) – Marcin Aug 04 '20 at 21:16