This project is my participation in the Nuwe Zurich Cloud Hackathon
These are the requirements of the hackathon:
- Create a Lambda function that automates inserting data into DynamoDB.
- Create Terraform files to automatically create all the necessary resources.
- Automate the execution of the Lambda function to be triggered when uploading a JSON file to an s3 Bucket.
- Create a short README with the infrastructure created and the decisions taken during the process.
This is a picture of the infrastructure we created with terraform.
The entire infrastructure is built with terraform and prepared to work with a terraform apply
.
Some terraform modules have been used to set up the infrastructure, there is documentation for each module in the README.md inside each folder module.
IAM and S3 policies have been created for the role we use in the Lambda function.
The lambda function created we use boto3 to connect to amazon services, json to be able to parse the json uploaded in the sales clients bucket and os to be able to import the name of the table, we created a variable for it in the terraform.
The script is on ./files/src/lambda_function.py
and when apply terraform the src content folder is zipped to ./files/lambda/lambda.zip
Before using this code, make sure you have the following configured:
Follow these steps to use the Terraform code:
-
Clone this repository on your local machine.
-
Copy the
terraform.tfvars.dist
file toterraform.tfvars
and adjust the configuration values to suit your needs. -
Run the following command in the root directory of the project:
terraform init
This will initialise the Terraform backend and download the necessary plugins.
-
Review the resources that will be created with the following command:
terraform plan
This will show you a description of the resources that will be created. On total 11 resources.
-
Finally, run the following command to create the resources in AWS:
terraform apply
Confirm the execution by typing "yes" when prompted.
The Terraform code is organised into the following files and directories:
main.tf
: This file contains the main Terraform configuration, including the AWS provider definition and the declaration of the modules used. Indlude all the resources, iam , s3, lambda and dynamodb.versions.tf
: This file defines the providers used.outputs.tf
: This file defines the outputs of the root module, which in this case there are none.variables.tf
: This file defines the variables used in the code, AWS region and AWS profile.modules/iam/role
: This directory contains theiam/role
module, which is responsible for creating the IAM role with the associated trust policy for lambda service.modules/iam/policy
: This directory contains theiam/policy
module, which is responsible for creating a policy and attach it to a role.modules/s3
: This directory contains thes3
module, which is used to create the S3 bucket. It also creates the S3 policy and gives permissions to the created Lambda IAM role.files
: This directory contains the folder for src and the lambda zip.