This repository contains sample applications showing how to use the AWS SAM CLI with Hashicorp's Terraform.
- AWS SAM CLI (Be sure to follow the instructions for the SAM CLI requirements)
- Docker or Docker equivelant
- Terraform by HashiCorp
The following instructions work for the Amazon API Gateway REST (v1) and HTTP (v2) APIs.
Choose the api you would like to test and change to that directory:
- REST (v1)
cd api_gateway_v1/tf-resources
- HTTP (v2)
cd api_gateway_v2/tf-resources
To ensure all Terraform dependencies are installed run the following:
terraform init
Each example contains two Lambda functions. One function is a simple application responder that returns a simple hello TF World
message, the other is an authorizer Lambda function used by API Gateway to authorize a user.
- Test the responder function:
sam local invoke 'module.lambda_function_responder.aws_lambda_function.this[0]'
- Test the authorizer function
sam local invoke 'module.lambda_function_auth.aws_lambda_function.this[0]' -e events/auth.json
Each project has two endpoints. One is an open endpoint and the other is secured by a Lambda authorizer for API Gateway. To test the endpoints start the local API with the following command:
sam local start-api
To test the open endpoint:
curl --location 'http://localhost:3000/open'
To test the secure endpoint as authorized:
curl --location 'http://localhost:3000/secure' --header 'myheader: 123456789'
To test the secure endpoint as unauthorized:
curl --location 'http://localhost:3000/secure' --header 'myheader: 123'
- Create a file in the folder of the version you would like to deploy called terraform.tfvars and add the following items if needed.
Optionally add the following if needed:
aws_region = "us-west-2"
profile = "your AWS profile" (defaults to 'default') config_location = "location of your AWS config file" (defaults to '~/.aws/config') creds_location = "location of your AWS creds file" (defaults to '~/.aws/credentials')
- Run the following to see what it will do:
terraform plan
- Run the following to deploy:
terraform apply
To destroy the application run the following command:
terraform destroy