Easy to set-up and flexible event-based data science pipelines.
We want to urge you to separate the infrastructure from the code. Use this only
to spin up and spin down infrastructure. The terraform modules will ignore most
changes you make to the infrastructure, including the code. You should use a
CI/CD pipeline for the code, the execution environment, environment variables.
If you run terraform apply
after updating the lambda function to use a new
image or other configuration updates, changes will mostly be ignored. This
pattern in inspired by
this article.
- Terraform
- AWS CLI v2
- Docker
- saml2aws
- AWS capability
- Incoming webhook connector url for Teams channel
- saml2aws login --force
aws s3api create-bucket --bucket <bucket_name> --region <region> --create-bucket-configuration LocationConstraint=<region>
- Enter terraform bucket name into backend.tf
- Start Docker
aws ecr get-login-password --region <region> | docker login --username AWS --password-stdin <account_id>.dkr.ecr.<region>.amazonaws.com
aws ecr create-repository --repository-name <repository_name>
- Enter folder the contains the Dockerfile that you want to build
docker build -t <repository_name> .
docker tag <repository_name>:<tag> <account_id>.dkr.ecr.<region>.amazonaws.com/<repository_name>:<tag>
docker push <account_id>.dkr.ecr.<region>.amazonaws.com/<repository_name>:<tag>
- In the terraform folder, define the infrastructure with the two source modules cronjob and sns_subscription.
- Write the webhook_url and monitor_image_uri in the terraform.tfvars file.
terraform init
terraform apply
You must have a file with a lambda handler function. You can also specify a requirements file to install all your needed dependencies. Below is an example of such a Dockerfile. Place it in the same directory as the code.
FROM amazon/aws-lambda-python:3.8
COPY requirements.txt preprocess.py ./
RUN python -m pip install -r requirements.txt -t .
CMD ["preprocess.lambda_handler"]
- Create ECR repository for the function and push the image.
- Use AWS CLI commands to update the function and other configuration associated to the function
aws lambda update-function-configuration --function-name <function_name> \
--environment "Variables={<env_name_1>=<env_name_1_val>, <env_name_2>=<env_name_2_val>}" \
--memory-size <memory_size> \
--timeout <timeout>
aws lambda update-function-code \
--function-name <function_name> \
--image-uri <image_uri>
aws iam attach-role-policy --policy-arn <policy_arn> --role-name <function_name>
When a lambda_cronjob or lambda_sns_subscription is spun up, terraform also spins up an SNS topic with the same name as the lambda function. When the lambda function is executed there are some predefined runtime environment variables: this includes the AWS_LAMBDA_FUNCTION_NAME, which contains the name of the lambda function. We use that to get the SNS topic ARN in the below example.
import json
import boto3
from os import environ
def get_sns_topic_arn():
account_id = boto3.client('sts').get_caller_identity().get('Account')
topic_name = environ["AWS_LAMBDA_FUNCTION_NAME"]
region = environ["AWS_REGION"]
return f"arn:aws:sns:{region}:{account_id}:{topic_name}"
def publish_to_sns(message):
client = boto3.client("sns")
sns_topic_arn = get_sns_topic_arn()
response = client.publish(
TargetArn=sns_topic_arn,
Message=json.dumps({"default": json.dumps(message)}),
MessageStructure="json",
)
where message
is a python dictionary.
Spin up the infrastructure for a two step cronjob -> sns_subscription pipeline.
- Start Docker
- Log in to capability..
- Note the account id (the 12 digit number next to the capability name)
- Create Terraform bucket.
cd modules/examples/monitor
aws ecr get-login-password --region eu-central-1 | docker login --username AWS --password-stdin <account_id>.dkr.ecr.eu-central-1.amazonaws.com
aws ecr create-repository --repository-name monitor_lambda
docker build -t monitor_lambda .
docker tag monitor_lambda:latest <account_id>.dkr.ecr.eu-central-1.amazonaws.com/monitor_lambda:latest
docker push <account_id>.dkr.ecr.eu-central-1.amazonaws.com/monitor_lambda:latest
- Return to the root of the repository:
cd ../../..
cd example
- Enter the webhook_url and monitor image uri in the terraform.tfvars file
- terraform init
- terraform apply