{"id":18368864,"url":"https://github.com/rpstreef/terraform-aws-sam-integration-example","last_synced_at":"2025-04-06T17:31:53.780Z","repository":{"id":40808890,"uuid":"261103288","full_name":"rpstreef/terraform-aws-sam-integration-example","owner":"rpstreef","description":"An example how you can use both Terraform and AWS SAM","archived":false,"fork":false,"pushed_at":"2022-12-06T00:42:13.000Z","size":423,"stargazers_count":35,"open_issues_count":7,"forks_count":13,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-22T04:03:11.148Z","etag":null,"topics":["aws","aws-codepipeline","aws-sam","codebuild","codedeploy","codepipeline","serverless","terraform"],"latest_commit_sha":null,"homepage":"","language":"HCL","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/rpstreef.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2020-05-04T07:05:10.000Z","updated_at":"2025-02-01T17:00:31.000Z","dependencies_parsed_at":"2023-01-23T11:30:43.472Z","dependency_job_id":null,"html_url":"https://github.com/rpstreef/terraform-aws-sam-integration-example","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rpstreef%2Fterraform-aws-sam-integration-example","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rpstreef%2Fterraform-aws-sam-integration-example/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rpstreef%2Fterraform-aws-sam-integration-example/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rpstreef%2Fterraform-aws-sam-integration-example/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/rpstreef","download_url":"https://codeload.github.com/rpstreef/terraform-aws-sam-integration-example/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247522429,"owners_count":20952546,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["aws","aws-codepipeline","aws-sam","codebuild","codedeploy","codepipeline","serverless","terraform"],"created_at":"2024-11-05T23:27:42.284Z","updated_at":"2025-04-06T17:31:53.321Z","avatar_url":"https://github.com/rpstreef.png","language":"HCL","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Terraform \u0026 AWS SAM integration example\n\nThis repo demonstrates how you can integrate Terraform infrastructure deployment with an [AWS SAM Application](https://github.com/rpstreef/aws-sam-node-example/). \n\nThe main reasons for this combination:\n\n- AWS SAM ease of local development and testing with official AWS developed Docker containers for AWS Lambda, API Gateway and DynamoDB.\n- AWS SAM concept of a Serverless Application that can be shared in their repository\n- AWS SAM and AWS CodeDeploy integration, allows for Blue/Green deployments with AWS CloudWatch alarms and deployment phase monitoring for the best deployment experience and reliability.\n- Terraform use of a modular approach resulting in reusable code (DRY). CloudFormation relies on hard to maintain includes or simply copy/paste of code.\n- Terraform wide range of AWS services support through AWS CLI as API. In some cases AWS CLI (and by extension, Terraform) supports certain AWS Services earlier than CloudFormation.\n\n# Get started\n\n## The essentials\n\n- Download Terraform v0.12.x [here](https://www.terraform.io/downloads.html)\n- You will need Node v12.x from [here](https://nodejs.org/en/download/)\n- Git, to clone this Repo, from [here](https://git-scm.com/downloads)\n- Create a free AWS account (requires credit card) [here](https://aws.amazon.com/)\n- Finally, download the [AWS CLI tool](https://aws.amazon.com/cli/) \n- Setup your AWS local profile, see [this](https://docs.aws.amazon.com/cli/latest/userguide/cli-configure-profiles.html) guide how it's done.\n- Manually setup an AWS S3 Bucket for Terraform state storage.\n\n## How to get it running\n\nIf you meet all the pre-requisites, do the following\n\n- In your AWS development account create the S3 bucket for your Terraform state files.\n  - Optionally, encrypt the S3 bucket and enable versioning such that you can do a rollback.\n- ``git clone`` this repo.\n- Change your AWS credentials profile name in these files: \n  - ``./env/dev/remote-backend.tf``\n  - ``./env/dev/dev.tfvars``\n- Run ``npm install`` and then execute ``npm run dev-init ``, this will:\n  - Initialize the Terraform project for the 'dev' environment, and synchronize the state with the cloud stored .tfstate file (when present).\n  - If you run it a second time, it will fail on the workspace creation, this is not an issue (the workspace already exists)\n- Run ``npm run dev-infra`` to prepare the deployment to your AWS account.\n  - Note: this [repo](https://github.com/rpstreef/aws-sam-node-example/) contains the AWS SAM template, AWS Lambda NodeJS source code, and the OpenAPI specification. These will automatically get deployed by AWS CodePipeline. But it requires a few steps after that to \"connect\" Terraform with AWS SAM.\n  - Confirm with ``yes`` to deploy, anything else will cancel the deployment\n  - The deployment will have errors; ``Error adding new Lambda Permission``, this is normal. It's because AWS SAM hasn't deployed the Lambda functions yet.\n- Run `dev-output-sam` to get a status update on the AWS SAM deployment in property ``StackStatus``\n  - When the deployment is finished, we can see the Stack output in the property ``Outputs``, match that with the ``dev.tfvars`` file.\n- Run ``dev-infra`` again after updating the ``dev.tfvars`` with the correct input\n  - Make sure the property ``Parameters`` corresponds with the output of Terraform. If it isn't the case update the ``configuration.json`` file in the AWS SAM example [repo](https://github.com/rpstreef/aws-sam-node-example/).\n- Redeploy Terraform with ``dev-infra``, this will add the AWS Lambda execution permissions to the API Gateway endpoints.\n\nThat's all done!\n\nSee my full guide on dev.to for more information about this project\n\n## VS Code plugins used\n\n- [StandardJS](https://marketplace.visualstudio.com/items?itemName=chenxsan.vscode-standardjs)\n- [Terraform](https://marketplace.visualstudio.com/items?itemName=mauve.terraform)\n- [OpenAPI Editor](https://marketplace.visualstudio.com/items?itemName=42Crunch.vscode-openapi)\n- [OpenAPI Designer](https://marketplace.visualstudio.com/items?itemName=philosowaffle.openapi-designer)\n\n## Running costs\n\nThere are no costs associated with deploying any of this on AWS, there is [Free Tier](https://aws.amazon.com/free) coverage for limited free use.\n\nThe following services are deployed with Terraform;\n- AWS Cognito\n- AWS IAM\n- AWS CloudWatch Alarms, costs will be incurred for enabling Detailed Monitoring for API Gateway (!)\n- AWS CodePipeline, CodeBuild, and CodeDeploy with Github as source repository. There's a free tier for:\n  - [CodeBuild](https://aws.amazon.com/codebuild/pricing/), 100 build minutes of ```build.general1.small``` per month.\n  - [AWS CodePipeline](https://aws.amazon.com/codepipeline/pricing/): 1 free pipeline active per month. New pipeline's free for the first 30 days.","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frpstreef%2Fterraform-aws-sam-integration-example","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Frpstreef%2Fterraform-aws-sam-integration-example","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frpstreef%2Fterraform-aws-sam-integration-example/lists"}