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.

1 Answer 1

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)
Sign up to request clarification or add additional context in comments.

3 Comments

Solved. Thank you!
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)[github.com/awsdocs/aws-doc-sdk-examples/blob/….
@LarenCrawford Thanks for letting me know :-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.