Skip to content

Commit f268d2f

Browse files
committed
Final notes and changes
1 parent f392134 commit f268d2f

File tree

6 files changed

+62
-32
lines changed

6 files changed

+62
-32
lines changed

README.md

+21-9
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
# LocalStack & Terraform REST API
22

3-
Develop rapidly for the cloud without the associated costs, slow feedback, and risk of actual deployments using LocalStack!
4-
5-
This is a guide on how to set up [Terraform](https://developer.hashicorp.com/terraform) and [LocalStack](https://localstack.cloud/) for local, [hot-reloading](https://docs.localstack.cloud/user-guide/tools/lambda-tools/hot-reloading/) Node.js AWS Lambda and API Gateway development. You'll be able to query your REST API entirely locally. The features used in this guide require you to have [LocalStack Pro](https://localstack.cloud/pricing/). There is a trial membership available.
3+
This is an example that uses [Terraform](https://developer.hashicorp.com/terraform) and [LocalStack](https://localstack.cloud/) to spin up a local [hot-reloading](https://docs.localstack.cloud/user-guide/tools/lambda-tools/hot-reloading/) Node.js [AWS Lambda](https://aws.amazon.com/lambda/) and [API Gateway](https://aws.amazon.com/api-gateway/) development environment. The features used in this guide require you to have [LocalStack Pro](https://localstack.cloud/pricing/). There is a trial membership available. Develop rapidly for the cloud without the associated costs, slow feedback, and risk of actual deployments. LocalStack also works with Pulumi, AWS CloudFormation/CDK, SAM, and more.
64

75
**In this guide we will:**
86

@@ -14,11 +12,25 @@ This is a guide on how to set up [Terraform](https://developer.hashicorp.com/ter
1412

1513
3. [Query your local REST API](./docs/query-your-api.md)
1614

17-
## Why do I need to have LocalStack Pro?
1815

19-
Some functionality like UpdateIntegration for API Gateway is not currently available for the community edition of LocalStack. It's possible to force the recreation of the API Gateway integration and get around this, but Pro includes a lot of other handy features, such as the [Resource Browser](https://docs.localstack.cloud/user-guide/web-application/resource-browser/) which is a web interface for browsing your LocalStack AWS resources.
2016

21-
# Final Questions
22-
1. What sort of workflows is this setup good for?
23-
2. What are some of the limitations of this setup?
24-
3. Is it better to run the lambda manually or through LocalStack?
17+
## Key Files/Directories
18+
19+
- 📁 /cdktf
20+
- Terraform CDK code used to create and manage AWS resources
21+
- 📁 /docs
22+
- Markdown documentation about the project
23+
- 📁 /lambda
24+
- Very simple lambda function that returns a JSON object `{ message: 'Hello World' }` and `200` http status code
25+
- 📄.env.example
26+
- File that contains LocalStack Pro key, copy this to a `.env` file
27+
- 📄main.tf
28+
- Primary Terraform HCL file used to create and manage AWS resources
29+
30+
31+
32+
33+
34+
## Why do I need to have LocalStack Pro?
35+
36+
Some functionality like [UpdateIntegration](https://docs.localstack.cloud/references/coverage/coverage_apigatewayv2/#updateintegration) for API Gateway is not currently available for the community edition of LocalStack. It's possible to force the recreation of the API Gateway integration and get around this, but Pro includes a lot of other handy features, such as the [Resource Browser](https://docs.localstack.cloud/user-guide/web-application/resource-browser/) which is a web interface for browsing your LocalStack AWS resources.

cdktf/src/localstack-config.ts

+14-7
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,18 @@ const endpoints = [
2929
];
3030

3131
export const AWS_CONFIG = {
32-
skipCredentialsValidation: true,
33-
skipMetadataApiCheck: "true",
34-
skipRequestingAccountId: true,
35-
accessKey: "fakeKeyForLocalTesting",
36-
secretKey: "fakeSecretForLocalTesting",
37-
region: "us-east-1",
38-
endpoints
32+
local: {
33+
skipCredentialsValidation: true,
34+
skipMetadataApiCheck: "true",
35+
skipRequestingAccountId: true,
36+
accessKey: "fakeKeyForLocalTesting",
37+
secretKey: "fakeSecretForLocalTesting",
38+
region: "us-east-1",
39+
endpoints
40+
},
41+
prod: {
42+
accessKey: "realKeyForProduction",
43+
secretKey: "realSecretForProduction",
44+
region: "us-east-1"
45+
}
3946
};

cdktf/src/main.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ class LambdaAPIStack extends TerraformStack {
3939

4040
new ArchiveProvider(this, 'archive', {});
4141

42-
new AwsProvider(this, 'aws', AWS_CONFIG);
42+
new AwsProvider(this, 'aws', STAGE === "local" ? AWS_CONFIG.local : AWS_CONFIG.prod);
4343

4444
const archiveFile = new DataArchiveFile(this, 'cdk_lambda_zip', {
4545
type: 'zip',
@@ -66,9 +66,9 @@ class LambdaAPIStack extends TerraformStack {
6666

6767
const fun = new LambdaFunction(this, 'cdk_lambda', {
6868
functionName: configValues.lambda.name,
69-
s3Bucket: STAGE == "local" ? "hot-reload" : null,
70-
s3Key: STAGE == "local" ? lambdaDirectory : null,
71-
filename: STAGE == "local" ? null : archiveFile.outputPath,
69+
s3Bucket: STAGE === "local" ? "hot-reload" : null,
70+
s3Key: STAGE === "local" ? lambdaDirectory : null,
71+
filename: STAGE === "local" ? null : archiveFile.outputPath,
7272
sourceCodeHash: archiveFile.outputBase64Sha256,
7373
handler: 'index.handler',
7474
runtime: 'nodejs18.x',

docs/initial-setup.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,24 @@ This setup will get you started with LocalStack and common functionality shared
55
To set up the project you'll need to:
66

77
1. Install Node and PNPM
8-
1. NVM will let you easily manage the version of Node you are currently using (I used v20 as of writing this)
8+
1. NVM will let you easily manage the version of Node you are currently using
99
1. [Windows NVM](https://github.com/coreybutler/nvm-windows)
1010
2. [Linux/MacOS NVM](https://github.com/nvm-sh/nvm)
1111
2. [PNPM](https://pnpm.io/installation) is a package manager for Node, like NPM and Yarn.
12-
1. If you prefer NPM or Yarn, you could delete the pnpm-lock.yaml files, but this could break the project!
12+
1. If you prefer NPM or Yarn, you can delete the pnpm-lock.yaml files, but this could break the project!
1313
2. [Install Docker](https://docs.docker.com/engine/install/)
1414
1. Localstack runs as a Docker container, so we need to [install Docker](https://docs.docker.com/engine/install/) first. Here's a handy GUI client for managing Docker containers, images, and volumes: [(Docker Desktop)](https://www.docker.com/products/docker-desktop).
1515
3. Install Python and pip
1616
1. Localstack is installed using Python's package manager, pip. You can download Python from [here](https://www.python.org/downloads/). Pip is included in Python 3.4 and above. But just in case here is pip (https://pypi.org/project/pip/)
1717
4. [Install LocalStack CLI](https://docs.localstack.cloud/getting-started/installation/)
1818
1. `python -m pip install localstack`
1919
5. [Generate a LocalStack API key](https://app.localstack.cloud/account/apikeys)
20-
6. Copy `.env.example` to `.env` and replace the `LOCALSTACK_API_KEY` value with the key you generated in the LocalStack web interface.
20+
6. Copy `.env.example` file to a new file named `.env` and replace the `LOCALSTACK_API_KEY` value with the key you generated in the LocalStack web interface.
2121
7. Install root project dependencies with PNPM
2222
1. Run `pnpm install` in the project root
2323
2. This installs the dotenv-cli which we use to read your LocalStack API token from the .env file. This is used to ensure you download and activate LocalStack Pro.
2424
8. Start the LocalStack Pro Docker container
2525
1. Run `pnpm run start:localstack` in the project root
26-
2. This convenience function reads you LocalStack API token and passes it to the LocalStack CLI command `localstack start -d`
26+
2. This convenience function reads your LocalStack API token and passes it to the LocalStack CLI command `localstack start -d`
2727
1. The `-d` flag starts LocalStack detached, so you can close your terminal without stopping the Docker container.
2828
9. You are now ready to configure your IaC provider, return to the project root [README](../README.md) and select your desired IaC provider.

docs/query-your-api.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Query Your REST API Endpoint
2-
1. Assemble an HTTP request using the API Gateway ID you noted earlier
3-
2. Syntax: `http://<API Gateway ID>.execute-api.localhost.localstack.cloud:4566/<stageId>/<path>`
2+
1. Assemble an HTTP request using the API Gateway ID you noted earlier in the IAC setup step ([Terraform HCL](./iac/terraform-hcl.md) OR [Terraform CDK](./iac/terraform-cdk.md))
3+
2. Endpoint Syntax: `http://<API Gateway ID>.execute-api.localhost.localstack.cloud:4566/<stageId>/<path>`
44
3. Example Endpoint: `http://1ltvwqpuju.execute-api.localhost.localstack.cloud:4566/local/test`
55
4. Example cURL:
66

main.tf

+17-6
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,23 @@
1+
// One option for differentiating environments is to use a variable
12
variable "STAGE" {
23
type = string
34
default = "local"
45
}
56

6-
// Set our AWS region
77
provider "aws" {
8+
// Set our AWS region
89
region = "us-east-1"
9-
skip_credentials_validation = true
10-
skip_metadata_api_check = true
11-
skip_requesting_account_id = true
12-
access_key = "fakeKeyForLocalTesting"
13-
secretKey = "fakeSecretForLocalTesting"
10+
11+
// Skip AWS credentials validation if we're running locally
12+
skip_credentials_validation = var.STAGE == "local"
13+
skip_metadata_api_check = var.STAGE == "local"
14+
skip_requesting_account_id = var.STAGE == "local"
15+
16+
// You can use these fake keys for local AWS testing
17+
access_key = var.STAGE == "local" ? "fakeKeyForLocalTesting" : "realKeyForProduction"
18+
secretKey = var.STAGE == "local" ? "fakeSecretForLocalTesting" : "realSecretForProduction"
19+
20+
// Configure LocalStack as our AWS endpoints if we're running locally
1421
endpoints {
1522
apigateway = var.STAGE == "local" ? "http://localhost:4566" : null
1623
cloudformation = var.STAGE == "local" ? "http://localhost:4566" : null
@@ -32,9 +39,13 @@ data "archive_file" "lambda_zip" {
3239
// Create the lambda function resource with our build ZIP file
3340
resource "aws_lambda_function" "lambda" {
3441
function_name = "myTestLambda"
42+
// This 'hot-reload' bucket name is used by LocalStack to mount our lambda code
3543
s3_bucket = var.STAGE == "local" ? "hot-reload" : null
44+
// This directory needs to be the location of the lambda code on your local machine
3645
s3_key = var.STAGE == "local" ? "${path.cwd}/lambda" : null
46+
// This is the name of the zip file we created above, this is only used in production
3747
filename = var.STAGE == "local" ? null : data.archive_file.lambda_zip.output_path
48+
// Used to determine if code has changed and the lambda needs to be redeployed
3849
source_code_hash = data.archive_file.lambda_zip.output_base64sha256
3950
handler = "index.handler"
4051
runtime = "nodejs18.x"

0 commit comments

Comments
 (0)