An open API service indexing awesome lists of open source software.

https://github.com/nyoungstudios/py-lambda-circleci

A template for a Python AWS Lambda function deployed automatically using the Serverless framework on CircleCI.
https://github.com/nyoungstudios/py-lambda-circleci

aws-cloudformation aws-lambda circleci python serverless

Last synced: 3 months ago
JSON representation

A template for a Python AWS Lambda function deployed automatically using the Serverless framework on CircleCI.

Awesome Lists containing this project

README

          

# Python AWS Lambda Serverless CircleCI Template

This is a simple template to setup a Python cron job as an AWS CloudFormation stack with a Lambda function and automatically deploy it using the Serverless framework on CircleCI. If you prefer to use GitHub Actions, check out this [template](https://github.com/nyoungstudios/py-lambda-gh-actions) instead.

## Features

This template uses a couple of Serverless plugins to make deploying the CloudFormation stack easier.
- [`serverless-layers`](https://www.serverless.com/plugins/serverless-layers) allows you to install the Python packages in the [requirements.txt](./requirements.txt) file to a separate Lambda layer which reduces the size of your code layer.
- [`serverless-deployment-bucket`](https://www.serverless.com/plugins/serverless-deployment-bucket) allows us to manage the S3 deployment bucket versioning and permissions.

Additionally, I have setup a simple [Python function](src/logging_setup.py) so you can setup a logger for your project without getting double prints in AWS CloudWatch.

## How to Setup?

1. Create a new repository based off of this template. Here is the GitHub [documentation](https://docs.github.com/en/github/creating-cloning-and-archiving-repositories/creating-a-repository-on-github/creating-a-repository-from-a-template) about creating a repository from a template.
2. Login to [CircleCI](https://app.circleci.com) and signin with your GitHub account.
3. Click the `Projects` tab, and then click the blue `Set Up Project` button corresponding to the new repository you just created.
4. In your AWS console, create a new [IAM policy](https://console.aws.amazon.com/iam/home?#/policies). If you are unsure what permissions to add to the policy, I've created a [minimal IAM policy](./circleci-aws-iam.json) with all the necessary permissions that CircleCI needs to deploy your CloudFormation stack. You can simply copy this to the AWS IAM policy JSON editor. Within the IAM policy JSON file, change `us-west-1` to whatever region you are using. Additionally change placeholder number `012345678901` to your AWS account number.
5. Next, create a [AWS User](https://console.aws.amazon.com/iam/home?#/users) based off of this policy. Set the `Access type` to `Programmatic access`. And on the permissions page, select `Attach existing policies directly` and search for the policy you just created in step 4.
6. Within the project on CircleCI, click the `Project Settings` button (on the top right side).
7. Within the `Project Settings`, click on the tab `Environment Variables` and set the environment variables `AWS_ACCESS_KEY_ID` and `AWS_SECRET_ACCESS_KEY` from the AWS User that you just created. Additionally, you can set the `AWS_DEFAULT_REGION` too.
8. And that's it! Now, whenever you push to your GitHub repository, your code will automatically be deployed as a CloudFormation stack.

## Python Versions

This template uses `Python 3.8`, but if you prefer to use a different Python version, just change `3.8` in [serverless.yml](./serverless.yml) and [config.yml](.circleci/config.yml) to whatever version you like. Here are the supported [AWS Lambda](https://docs.aws.amazon.com/lambda/latest/dg/lambda-runtimes.html) Python runtimes. Also, here are the CircleCI Python [Docker images](https://circleci.com/developer/images/image/cimg/python) that you can for deploying.