Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 3 additions & 6 deletions .github/workflows/makefile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,12 @@ jobs:
terraform_version: 1.8.4
terraform_wrapper: false

- name: Install LocalStack
run: pip install localstack awscli-local[ver1] terraform-local

- name: Pull the latest docker image
run: docker pull localstack/localstack-pro
- name: Install lstk
run: npm install -g @localstack/lstk

- name: Execute tests
env:
LOCALSTACK_API_KEY: ${{ secrets.LOCALSTACK_API_KEY }}
LOCALSTACK_AUTH_TOKEN: ${{ secrets.LOCALSTACK_AUTH_TOKEN }}
DEBUG: 1
timeout-minutes: 100
run: make test-ci-all
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ __pycache__

.idea
logs.txt
cluster_info.json
18 changes: 7 additions & 11 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,29 +1,25 @@
lint:
@echo "==> Linting terraform code"
@terraform fmt -diff=true -recursive -write=true
LSTK := lstk --non-interactive

usage: ## Show this help
@fgrep -h "##" $(MAKEFILE_LIST) | fgrep -v fgrep | sed -e 's/\\$$//' | sed -e 's/##//'

install: ## Install dependencies for all projects
MAKE_TARGET='install' make for-each-dir

lint: ## Run code linter for all projects
MAKE_TARGET='lint' make for-each-dir
lint: ## Run code linter for all projects (skips sample-archive)
@echo "==> Linting terraform code"
@find . -name '*.tf' -not -path './sample-archive/*' -exec dirname {} \; | sort -u | xargs -I{} terraform fmt -diff=true -write=true {}

start: ## Start LocalStack infrastructure
localstack start -d

ready: ## Check if the LocalStack container is up and running.
localstack wait -t 20 && echo "LocalStack is ready to use!"
$(LSTK) start

stop: ## Stop LocalStack infrastructure
localstack stop
$(LSTK) stop

for-each-dir:
./make-for-each.sh $$MAKE_TARGET $$CMD

test-ci-all:
MAKE_TARGET='test-ci' make for-each-dir

.PHONY: usage install lint start ready stop for-each-dir test-ci-all
.PHONY: usage install lint start stop for-each-dir test-ci-all
7 changes: 3 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ Each example in the repository is prefixed with the AWS service being used. For
## Prerequisites

- [Docker](https://docs.docker.com/get-docker/)
- [`localstack` CLI](https://docs.localstack.cloud/getting-started/installation/#localstack-cli)
- [`awslocal` CLI](https://docs.localstack.cloud/user-guide/integrations/aws-cli/)
- [`lstk` CLI](https://docs.localstack.cloud/aws/tooling/lstk/) — provides `lstk aws` and `lstk terraform` wrappers
- [Terraform](https://developer.hashicorp.com/terraform/downloads)
- `make` and `jq`

Expand All @@ -22,10 +21,10 @@ Set it before running any sample:
export LOCALSTACK_AUTH_TOKEN=<your-auth-token>
```

Alternatively, use the LocalStack CLI to persist the token:
Alternatively, use the `lstk` CLI to log in and store your credentials in the system keyring:

```bash
localstack auth set-token <your-auth-token>
lstk login
```

You can find your auth token in the [LocalStack Web Application](https://app.localstack.cloud/workspace/auth-token).
Expand Down
49 changes: 2 additions & 47 deletions acm-route53/Makefile
Original file line number Diff line number Diff line change
@@ -1,48 +1,3 @@
export AWS_ACCESS_KEY_ID ?= test
export AWS_SECRET_ACCESS_KEY ?= test
export AWS_DEFAULT_REGION = us-east-1
SHELL := /bin/bash
PYTHON_BIN ?= $(shell which python3 || which python)
TEST_CMD = @$(LSTK) aws acm list-certificates | jq '.CertificateSummaryList[] | select(.CertificateArn != null and .DomainName != null)' >/dev/null && echo "Certificates found with CertificateArn and DomainName" || (echo "No certificates found with CertificateArn and DomainName" && exit 1)

usage: ## Show this help
@fgrep -h "##" $(MAKEFILE_LIST) | fgrep -v fgrep | sed -e 's/\\$$//' | sed -e 's/##//'

start:
@test -n "${LOCALSTACK_AUTH_TOKEN}" || (echo "LOCALSTACK_AUTH_TOKEN is not set" && exit 1);
DEBUG=1 LOCALSTACK_AUTH_TOKEN=$(LOCALSTACK_AUTH_TOKEN) localstack start -d

stop:
localstack stop

ready:
@echo Waiting on the LocalStack container...
@localstack wait -t 30 && echo Localstack is ready to use! || (echo Gave up waiting on LocalStack, exiting. && exit 1)

logs:
@localstack logs > logs.txt

install: ## Install dependencies
@which localstack || pip install localstack
@which awslocal || pip install awscli-local
@which terraform || (echo 'Terraform was not found';)
@which tflocal || pip install terraform-local

init:
tflocal init

deploy:
tflocal apply --auto-approve

test:
@awslocal acm list-certificates | jq '.CertificateSummaryList[] | select(.CertificateArn != null and .DomainName != null)' >/dev/null && echo "Certificates found with CertificateArn and DomainName" || (echo "No certificates found with CertificateArn and DomainName" && exit 1)

run: start install init deploy test

test-ci:
make start install init deploy test; return_code=`echo $$?`;\
make logs; make stop; exit $$return_code;

clean:
rm -rf .terraform

.PHONY: start ready install init deploy test logs run test-ci clean
include ../common.mk
12 changes: 5 additions & 7 deletions acm-route53/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ This example shows how to generate an ACM certificate for a domain via Terraform

- A valid [LocalStack for AWS license](https://localstack.cloud/pricing). Your license provides a [`LOCALSTACK_AUTH_TOKEN`](https://docs.localstack.cloud/getting-started/auth-token/).
- [Docker](https://docs.docker.com/get-docker/)
- [`localstack` CLI](https://docs.localstack.cloud/getting-started/installation/#localstack-cli)
- [`awslocal` CLI](https://docs.localstack.cloud/user-guide/integrations/aws-cli/)
- [`lstk` CLI](https://docs.localstack.cloud/aws/tooling/lstk/)
- [Terraform](https://developer.hashicorp.com/terraform/downloads)
- `make` and `jq`

Expand All @@ -16,25 +15,24 @@ This example shows how to generate an ACM certificate for a domain via Terraform
```bash
export LOCALSTACK_AUTH_TOKEN=<your-auth-token>
make start
make ready
```

## Run

To run this example you need to execute:

```bash
tflocal init
tflocal plan
tflocal apply --auto-approve
lstk tf init
lstk tf plan
lstk tf apply --auto-approve
```

## Testing

Run the following command to test the example:

```bash
awslocal acm list-certificates
lstk aws acm list-certificates
```

You will see the following output:
Expand Down
49 changes: 1 addition & 48 deletions apigateway-dynamodb/Makefile
Original file line number Diff line number Diff line change
@@ -1,48 +1 @@
export AWS_ACCESS_KEY_ID ?= test
export AWS_SECRET_ACCESS_KEY ?= test
export AWS_DEFAULT_REGION = us-east-1
SHELL := /bin/bash
PYTHON_BIN ?= $(shell which python3 || which python)

usage: ## Show this help
@fgrep -h "##" $(MAKEFILE_LIST) | fgrep -v fgrep | sed -e 's/\\$$//' | sed -e 's/##//'

start:
@test -n "${LOCALSTACK_AUTH_TOKEN}" || (echo "LOCALSTACK_AUTH_TOKEN is not set" && exit 1);
DEBUG=1 LOCALSTACK_AUTH_TOKEN=$(LOCALSTACK_AUTH_TOKEN) localstack start -d

stop:
localstack stop

ready:
@echo Waiting on the LocalStack container...
@localstack wait -t 30 && echo Localstack is ready to use! || (echo Gave up waiting on LocalStack, exiting. && exit 1)

logs:
@localstack logs > logs.txt

install: ## Install dependencies
@which localstack || pip install localstack
@which awslocal || pip install awscli-local
@which terraform || (echo 'Terraform was not found';)
@which tflocal || pip install terraform-local

init:
tflocal init

deploy:
tflocal apply --auto-approve

test:
./run.sh

run: start install init deploy test

test-ci:
make start install init deploy test; return_code=`echo $$?`;\
make logs; make stop; exit $$return_code;

clean:
rm -rf .terraform

.PHONY: start ready install init deploy test logs run test-ci clean
include ../common.mk
4 changes: 1 addition & 3 deletions apigateway-dynamodb/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ Localstack sample using Apigateway with DynamoDB backend

- A valid [LocalStack for AWS license](https://localstack.cloud/pricing). Your license provides a [`LOCALSTACK_AUTH_TOKEN`](https://docs.localstack.cloud/getting-started/auth-token/).
- [Docker](https://docs.docker.com/get-docker/)
- [`localstack` CLI](https://docs.localstack.cloud/getting-started/installation/#localstack-cli)
- [`awslocal` CLI](https://docs.localstack.cloud/user-guide/integrations/aws-cli/)
- [`lstk` CLI](https://docs.localstack.cloud/aws/tooling/lstk/)
- [Terraform](https://developer.hashicorp.com/terraform/downloads)
- `make` and `jq`

Expand All @@ -19,7 +18,6 @@ Localstack sample using Apigateway with DynamoDB backend
```bash
export LOCALSTACK_AUTH_TOKEN=<your-auth-token>
make start
make ready
```

### Run
Expand Down
11 changes: 7 additions & 4 deletions apigateway-dynamodb/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,11 @@ resource "aws_dynamodb_table" "MyDynamoDBTable" {
}

global_secondary_index {
name = "PetType-index"
hash_key = "PetType"
name = "PetType-index"
key_schema {
attribute_name = "PetType"
key_type = "HASH"
}
write_capacity = 5
read_capacity = 5
projection_type = "INCLUDE"
Expand Down Expand Up @@ -103,7 +106,7 @@ resource "aws_api_gateway_rest_api" "MyApiGatewayRestApi" {
"type" : "aws",
"credentials" : "${aws_iam_role.APIGWRole.arn}",
"httpMethod" : "POST",
"uri" : "arn:aws:apigateway:${data.aws_region.current.name}:dynamodb:action/PutItem",
"uri" : "arn:aws:apigateway:${data.aws_region.current.region}:dynamodb:action/PutItem",
"responses" : {
"default" : {
"statusCode" : "200",
Expand Down Expand Up @@ -141,7 +144,7 @@ resource "aws_api_gateway_rest_api" "MyApiGatewayRestApi" {
"type" : "aws",
"credentials" : "${aws_iam_role.APIGWRole.arn}",
"httpMethod" : "POST",
"uri" : "arn:aws:apigateway:${data.aws_region.current.name}:dynamodb:action/Query",
"uri" : "arn:aws:apigateway:${data.aws_region.current.region}:dynamodb:action/Query",
"responses" : {
"default" : {
"statusCode" : "200",
Expand Down
4 changes: 2 additions & 2 deletions apigateway-dynamodb/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

set -x

apikey=$(tflocal output -json | jq -r .apigw_key.value)
apikey=$(lstk tf output -json | jq -r .apigw_key.value)

sleep 5

restapi=$(aws apigateway --endpoint-url=http://localhost:4566 get-rest-apis | jq -r .items[0].id)
restapi=$(lstk aws apigateway get-rest-apis | jq -r .items[0].id)
curl $restapi.execute-api.localhost.localstack.cloud:4566/v1/pets -H "x-api-key: ${apikey}" -H 'Content-Type: application/json' --request POST --data-raw '{ "PetType": "dog", "PetName": "tito", "PetPrice": 250 }'

response=$(curl -H "x-api-key: ${apikey}" --request GET $restapi.execute-api.localhost.localstack.cloud:4566/v1/pets/dog)
Expand Down
49 changes: 1 addition & 48 deletions apigateway-http-proxy-authorizer/Makefile
Original file line number Diff line number Diff line change
@@ -1,48 +1 @@
export AWS_ACCESS_KEY_ID ?= test
export AWS_SECRET_ACCESS_KEY ?= test
export AWS_DEFAULT_REGION = us-east-1
SHELL := /bin/bash
PYTHON_BIN ?= $(shell which python3 || which python)

usage: ## Show this help
@fgrep -h "##" $(MAKEFILE_LIST) | fgrep -v fgrep | sed -e 's/\\$$//' | sed -e 's/##//'

start:
@test -n "${LOCALSTACK_AUTH_TOKEN}" || (echo "LOCALSTACK_AUTH_TOKEN is not set" && exit 1);
DEBUG=1 LOCALSTACK_AUTH_TOKEN=$(LOCALSTACK_AUTH_TOKEN) localstack start -d

stop:
localstack stop

ready:
@echo Waiting on the LocalStack container...
@localstack wait -t 30 && echo Localstack is ready to use! || (echo Gave up waiting on LocalStack, exiting. && exit 1)

logs:
@localstack logs > logs.txt

install: ## Install dependencies
@which localstack || pip install localstack
@which awslocal || pip install awscli-local
@which terraform || (echo 'Terraform was not found';)
@which tflocal || pip install terraform-local

init:
tflocal init

deploy:
tflocal apply --auto-approve

test:
./run.sh

run: start install init deploy test

test-ci:
make start install init deploy test; return_code=`echo $$?`;\
make logs; make stop; exit $$return_code;

clean:
rm -rf .terraform

.PHONY: start ready install init deploy test logs run test-ci clean
include ../common.mk
4 changes: 1 addition & 3 deletions apigateway-http-proxy-authorizer/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ This sample demonstrates Terraform-based AWS service emulation with LocalStack.

- A valid [LocalStack for AWS license](https://localstack.cloud/pricing). Your license provides a [`LOCALSTACK_AUTH_TOKEN`](https://docs.localstack.cloud/getting-started/auth-token/).
- [Docker](https://docs.docker.com/get-docker/)
- [`localstack` CLI](https://docs.localstack.cloud/getting-started/installation/#localstack-cli)
- [`awslocal` CLI](https://docs.localstack.cloud/user-guide/integrations/aws-cli/)
- [`lstk` CLI](https://docs.localstack.cloud/aws/tooling/lstk/)
- [Terraform](https://developer.hashicorp.com/terraform/downloads)
- `make` and `jq`

Expand All @@ -16,7 +15,6 @@ This sample demonstrates Terraform-based AWS service emulation with LocalStack.
```bash
export LOCALSTACK_AUTH_TOKEN=<your-auth-token>
make start
make ready
```

## Run Sample
Expand Down
26 changes: 25 additions & 1 deletion apigateway-http-proxy-authorizer/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ resource "aws_api_gateway_integration" "integration" {
http_method = aws_api_gateway_method.method_get.http_method

type = "HTTP_PROXY"
integration_http_method = "POST"
integration_http_method = "GET"
uri = "http://httpbin.org/anything/{proxy}"
}

Expand Down Expand Up @@ -97,3 +97,27 @@ resource "aws_iam_role" "role" {
}
POLICY
}

resource "aws_api_gateway_deployment" "deployment" {
rest_api_id = aws_api_gateway_rest_api.rest.id

triggers = {
redeployment = sha1(jsonencode([
aws_api_gateway_resource.proxy.id,
aws_api_gateway_method.method_get.id,
aws_api_gateway_method.method_post.id,
aws_api_gateway_integration.integration.id,
aws_api_gateway_integration.integration_post.id,
]))
}

lifecycle {
create_before_destroy = true
}
}

resource "aws_api_gateway_stage" "local" {
rest_api_id = aws_api_gateway_rest_api.rest.id
deployment_id = aws_api_gateway_deployment.deployment.id
stage_name = "local"
}
Loading