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 with GitHub Actions. If you prefer to use CircleCI, check out this template instead.
This template uses a couple of Serverless plugins to make deploying the CloudFormation stack easier.
serverless-layers
allows you to install the Python packages in the requirements.txt file to a separate Lambda layer which reduces the size of your code layer.serverless-deployment-bucket
allows us to manage the S3 deployment bucket versioning and permissions.
Additionally, I have setup a simple Python function so you can setup a logger for your project without getting double prints in AWS CloudWatch.
- Create a new repository based off of this template. Here is the GitHub documentation about creating a repository from a template.
- In your AWS console, create a new IAM policy. If you are unsure what permissions to add to the policy, I've created a minimal IAM policy with all the necessary permissions that GitHub Actions 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 number012345678901
to your AWS account number. - Next, create a AWS User based off of this policy. Set the
Access type
toProgrammatic access
. And on the permissions page, selectAttach existing policies directly
and search for the policy you just created in step 4. - Within the
Settings
tab on your GitHub repository, click on theSecrets
tab and set the environment variablesAWS_ACCESS_KEY_ID
andAWS_SECRET_ACCESS_KEY
from the AWS User that you just created. Additionally, you can set theAWS_DEFAULT_REGION
too. - And that's it! Now, whenever you push to your GitHub repository, your code will automatically be deployed as a CloudFormation stack.
This template uses Python 3.8
, but if you prefer to use a different Python version, just change 3.8
in serverless.yml and deploy.yml to whatever version you like. Here are the supported AWS Lambda Python runtimes.