NOTE: This code was written for an article in the RockedScience publication on Medium.
This tutorial shows how to deploy a Python function on
AWS Lambda with
Terraform and
Terragrunt.
The Terraform configuration can also:
- install the required
Pythondependencies - keep track of changes to the code and dependencies to avoid re-deploying an unchanged function
Terragrunt is a wrapper for Terraform which helps
to manage multiple environments.
Optional:
- Create an AWS account and get a
Access Key IDandSecret Access Keypair (read the docs) - Install and configure the
AWS CLI(read the docs) - Install
Terraform(read the docs) - Install
Pythonor, withMinicondaorConda, run:(Python docs)$ conda env create -n tflambda -f environment.yml $ source activate tflambda Terragruntis optional, install it for a more advanced setup (read the docs)
$ cd lambda
$ pip install -r requirements.txt
$ python lambda.py
to view the log output on screen, run:
$ LOG_LEVEL=INFO python lambda.py
Run:
$ cd terraform
$ terraform init
$ terraform apply
If you have multiple AWS profiles and you want to set the profile to use:
$ AWS_PROFILE=myprofilename terraform apply
The Terraform AWS provider offers a number of options to set credentials,
check the docs
for further details.
To deploy to a specific AWS region
(see variables.tf for defaults):
$ terraform apply -var aws_region="eu-west-1"
2 sample Terragrunt configurations are provided:
terragrunt/eusets the AWSregiontoeu-west-1terragrunt/ussets the AWSregiontous-east-1
Run:
$ cd terragrunt/eu
$ terragrunt apply
change /eu to /us and repeat to try another region.
AWS profiles selection also applies to Terragrunt:
$ AWS_PROFILE=myprofilename terragrunt apply
Run:
$ cd terraform
$ terraform destroy