Skip to content

Commit 7a4e007

Browse files
authored
Merge pull request #1 from localstack-samples/review-1
Review and updates
2 parents 86e6291 + 7c4af3f commit 7a4e007

File tree

16 files changed

+1139
-6113
lines changed

16 files changed

+1139
-6113
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,4 @@
11
.vscode
2+
.idea
3+
iac/awscdk/output.json
4+
.env-gdc-local

Makefile

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,20 @@
1+
SHELL := /bin/bash
2+
3+
-include .env-gdc-local
4+
15
CDIR = cd iac/awscdk
26

7+
export APP_NAME?=ecslb
8+
export ENFORCE_IAM?=0
9+
310
usage: ## Show this help in table format
411
@echo "| Target | Description |"
512
@echo "|------------------------|-------------------------------------------------------------------|"
613
@fgrep -h "##" $(MAKEFILE_LIST) | fgrep -v fgrep | sed -e 's/:.*##\s*/##/g' | awk -F'##' '{ printf "| %-22s | %-65s |\n", $$1, $$2 }'
714

815

916
deploy: ## Bootstrap and deploy the CDK app to AWS
10-
${CDIR}; cdk bootstrap; cdk deploy --outputs-file ./ouput.json --json
17+
${CDIR}; cdk bootstrap; cdk deploy --outputs-file ./output.json --json
1118

1219
destroy: ## Destroy the deployed CDK stack in AWS
1320
${CDIR}; cdk destroy
@@ -17,12 +24,36 @@ destroy-local: ## Destroy the deployed CDK stack locally
1724

1825
deploy-local: ## Bootstrap and deploy the CDK app locally
1926
${CDIR}; cdklocal bootstrap && \
20-
cdklocal deploy --outputs-file ./ouput.json --json --require-approval never
27+
cdklocal deploy --outputs-file ./output.json --json --require-approval never
2128

2229
test: ## Run integration tests
23-
npm run test
30+
cd tests && npm run test
31+
32+
test-local: ## Run integration tests
33+
cd tests && LOCALSTACK=1 npm run test
34+
35+
curl-local:
36+
curl $(shell cat iac/awscdk/output.json | jq '.RepoStack.localstackserviceslb')
37+
38+
curl-aws:
39+
curl $(shell cat iac/awscdk/output.json | jq '.RepoStack.serviceslb')
2440

2541
install: ## Install npm dependencies
2642
${CDIR}; npm install
2743

28-
.PHONY: usage install test deploy destroy deploy-local destroy-local bootstrap-local
44+
45+
start-localstack:
46+
cd devops-tooling && docker compose -p $(APP_NAME) up
47+
48+
stop-localstack:
49+
cd devops-tooling && docker compose down
50+
51+
52+
PKG_SUB_DIRS := $(dir $(shell find . -type d -name node_modules -prune -o -type d -name "venv*" -prune -o -type f -name package.json -print))
53+
54+
update-deps: $(PKG_SUB_DIRS)
55+
for i in $(PKG_SUB_DIRS); do \
56+
pushd $$i && ncu -u && npm install && popd; \
57+
done
58+
59+
.PHONY: usage install test test-local deploy destroy deploy-local destroy-local bootstrap-local update-deps start-localstack stop-localstack curl-local curl-aws

README.md

Lines changed: 74 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,43 @@
11
### 🌐 Overview
22

3-
The sample application, utilizing the AWS Cloud Development Kit (AWS CDK) 🛠️, orchestrates the deployment of a containerized application 📦 on AWS Fargate within an Amazon ECS (Elastic Container Service) cluster. The CDK infrastructure-as-code model allows developers to define cloud resources using familiar programming languages 🖥️.
3+
The sample application, utilizing the AWS Cloud Development Kit (AWS CDK) 🛠️, orchestrates the deployment of a
4+
containerized application 📦 on AWS Fargate within an Amazon ECS (Elastic Container Service) cluster. The CDK
5+
infrastructure-as-code model allows developers to define cloud resources using familiar programming languages 🖥️.
46

57
### 🔑 Key Components
68

7-
- **🌍 VPC and Cluster:**
8-
The script initiates a new Virtual Private Cloud (VPC) and an ECS Cluster, ensuring a secure 🔐 and isolated network environment and a logical grouping of ECS tasks and services, respectively.
9+
- **🌍 VPC and Cluster:**
10+
The script initiates a new Virtual Private Cloud (VPC) and an ECS Cluster, ensuring a secure 🔐 and isolated network
11+
environment and a logical grouping of ECS tasks and services, respectively.
912

10-
- **🐳 Docker Image Asset:**
11-
The `DockerImageAsset` class is used to build a Docker image from a local directory (specified path) and push it to Amazon Elastic Container Registry (ECR). The built image is then used in the ECS task definition.
13+
- **🐳 Docker Image Asset:**
14+
The `DockerImageAsset` class is used to build a Docker image from a local directory (specified path) and push it to
15+
Amazon Elastic Container Registry (ECR). The built image is then used in the ECS task definition.
1216

13-
- **🚀 Task Definition and Container Definition:**
14-
An ECS task definition is created, specifying the Docker image to use, CPU 🖥️, and memory requirements, network mode, and logging configuration. A container within this task is defined, specifying port mappings and essential status.
17+
- **🚀 Task Definition and Container Definition:**
18+
An ECS task definition is created, specifying the Docker image to use, CPU 🖥️, and memory requirements, network mode,
19+
and logging configuration. A container within this task is defined, specifying port mappings and essential status.
1520

16-
- **🛳️ ECS Fargate Service:**
17-
The ECS service is configured to run on Fargate, which allows running containers without managing the underlying instances. The service is configured with the above task definition, desired count of tasks, and a circuit breaker for handling failures.
21+
- **🛳️ ECS Fargate Service:**
22+
The ECS service is configured to run on Fargate, which allows running containers without managing the underlying
23+
instances. The service is configured with the above task definition, desired count of tasks, and a circuit breaker for
24+
handling failures.
1825

19-
- **⚖️ Application Load Balancer (ALB):**
20-
An ALB is provisioned to distribute incoming HTTP/S traffic across multiple targets, such as ECS tasks, in multiple Availability Zones. A listener is added to the load balancer to check for connection requests from clients, using the HTTP protocol and listening on port 80.
26+
- **⚖️ Application Load Balancer (ALB):**
27+
An ALB is provisioned to distribute incoming HTTP/S traffic across multiple targets, such as ECS tasks, in multiple
28+
Availability Zones. A listener is added to the load balancer to check for connection requests from clients, using the
29+
HTTP protocol and listening on port 80.
2130

22-
- **🎯 Target Group and Health Checks:**
23-
Targets (in this case, the ECS service) are registered with a target group, which the ALB uses to route traffic to. Health check settings ensure that traffic is routed only to healthy targets.
31+
- **🎯 Target Group and Health Checks:**
32+
Targets (in this case, the ECS service) are registered with a target group, which the ALB uses to route traffic to.
33+
Health check settings ensure that traffic is routed only to healthy targets.
2434

2535
## Getting Started 🏁
2636

27-
This guide assumes that you have cloned the repository and are in the project root directory. The following steps will guide you through the process of building, deploying, and running the sample application both locally and on AWS. We have a [sample application](https://github.com/localstack-samples/sample-cdk-ecs-elb/tree/main/src) that listens on port `3000` and returns a JSON response.
37+
This guide assumes that you have cloned the repository and are in the project root directory. The following steps will
38+
guide you through the process of building, deploying, and running the sample application both locally and on AWS. We
39+
have a [sample application](https://github.com/localstack-samples/sample-cdk-ecs-elb/tree/main/src) that listens on
40+
port `3000` and returns a JSON response.
2841

2942
### Prerequisites 🧰
3043

@@ -33,7 +46,7 @@ This guide assumes that you have cloned the repository and are in the project ro
3346
- [AWS CDK](https://docs.aws.amazon.com/cdk/latest/guide/getting_started.html#getting_started_install)
3447
- [Node.js](https://nodejs.org/en/download/)
3548
- [cdklocal](https://docs.localstack.cloud/user-guide/integrations/aws-cdk/)
36-
49+
- [jq](https://jqlang.github.io/jq/download/)
3750

3851
### Install Dependencies 📦
3952

@@ -51,11 +64,9 @@ Start the LocalStack server using the following command:
5164

5265
```bash
5366
export LOCALSTACK_API_KEY=<YOUR_API_KEY>
54-
DEBUG=1 localstack start
67+
make start-localstack
5568
```
5669

57-
We specified `DEBUG=1` to get the printed LocalStack logs directly in the terminal to help us visualize the background tasks in action. If you prefer running LocalStack in detached mode, you can add the `-d` flag to the `localstack start` command, and use Docker Desktop to view the logs.
58-
5970
### Step 2: Deploy the Application 🚢
6071

6172
Deploy your application locally using the following command:
@@ -71,12 +82,14 @@ This command will deploy your application to the LocalStack. Ensure that there a
7182
Run the application test cases using the following command:
7283

7384
```bash
74-
make test
85+
make test-local
7586
```
7687

77-
Ensure that all test cases pass and pay attention to any output that is displayed. This step should validate that the application is functioning as expected.
88+
Ensure that all test cases pass and pay attention to any output that is displayed. This step should validate that the
89+
application is functioning as expected.
7890

79-
Alternatively, you can also check the application by curling the ALB endpoint. You can find the ALB endpoint in the LocalStack console or by running the following command:
91+
Alternatively, you can also check the application by curling the ALB endpoint. You can find the ALB endpoint in the
92+
LocalStack console or by running the following command:
8093

8194
```bash
8295
awslocal elbv2 describe-load-balancers --names serviceslb --query 'LoadBalancers[0].DNSName'
@@ -85,7 +98,7 @@ awslocal elbv2 describe-load-balancers --names serviceslb --query 'LoadBalancers
8598
Now you can curl the endpoint using the following command:
8699

87100
```bash
88-
curl serviceslb.elb.localhost.localstack.cloud:4566
101+
make curl-local
89102
```
90103

91104
The output should be similar to the following:
@@ -114,24 +127,19 @@ make deploy
114127

115128
This command will deploy your application to AWS. Ensure that there are no errors.
116129

117-
### Step 2: Run Test Cases 🧪
130+
### Step 2: Run Test Cases Against AWS🧪
118131

119132
Run the application test cases using the following command:
120133

121134
```bash
122135
make test
123136
```
124137

125-
Alternatively, you can also check the application by curling the ALB endpoint. You can find the ALB endpoint in the AWS console or by running the following command:
138+
Alternatively, you can also check the application by curling the ALB endpoint. You can find the ALB endpoint in the AWS
139+
console or by running the following command:
126140

127141
```bash
128-
aws elbv2 describe-load-balancers --names serviceslb --query 'LoadBalancers[0].DNSName' --output text
129-
```
130-
131-
Now you can curl the endpoint using the following command:
132-
133-
```bash
134-
curl <ALB_ENDPOINT-FROM_ABOVE_COMMAND>
142+
make curl-aws
135143
```
136144

137145
The output should be similar to the following:
@@ -150,58 +158,64 @@ make destroy
150158

151159
## 🚀 Configuring Visual Studio Code for Efficient Remote Node.js Debugging
152160

153-
Setting up Visual Studio Code for remote Node.js debugging enables smoother and more intuitive development workflows. This guide will walk you through the essential steps to configure your VSCode efficiently for remote debugging of your Node.js applications. 🛠️🔍
161+
Setting up Visual Studio Code for remote Node.js debugging enables smoother and more intuitive development workflows.
162+
This guide will walk you through the essential steps to configure your VSCode efficiently for remote debugging of your
163+
Node.js applications. 🛠️🔍
154164

155165
1️⃣ **Configure LocalStack for remote Node.js debugging** 🛠️
156166

157-
First, we need to configure LocalStack to enable remote debugging of Node.js applications. To do so, we need to set the `ECS_DOCKER_FLAGS` to enable the debugger using `NODE_OPTIONS`:
158-
167+
First, we need to configure LocalStack to enable remote debugging of Node.js applications. To do so, we need to set
168+
the `ECS_DOCKER_FLAGS` to enable the debugger using `NODE_OPTIONS`:
169+
159170
```bash
160171
export ECS_DOCKER_FLAGS="e NODE_OPTIONS=--inspect-brk=0.0.0.0:9229 -p 9229:9229
161172
```
162173
163174
2️⃣ **Adding a Task to Wait for Remote Debugger Server** 🕰️
164175
165-
First, let's ensure that VSCode waits for the remote debugger server to be available. Add a new task by creating or modifying the `.vscode/tasks.json` file in your project directory.
176+
First, let's ensure that VSCode waits for the remote debugger server to be available. Add a new task by creating or
177+
modifying the `.vscode/tasks.json` file in your project directory.
166178
167179
```json
168180
{
169-
"version": "2.0.0",
170-
"tasks": [
171-
{
172-
"label": "Wait Remote Debugger Server",
173-
"type": "shell",
174-
"command": "while [[ -z $(docker ps | grep :9229) ]]; do sleep 1; done; sleep 1;"
175-
}
176-
]
177-
}
181+
"version": "2.0.0",
182+
"tasks": [
183+
{
184+
"label": "Wait Remote Debugger Server",
185+
"type": "shell",
186+
"command": "while [[ -z $(docker ps | grep :9229) ]]; do sleep 1; done; sleep 1;"
187+
}
188+
]
189+
}
178190
```
179191
180192
3️⃣ **Setting up Debugging Configuration** 🎛️
181193
182-
Next, define how VSCode should connect to the remote Node.js application. Create a new `launch.json` file or modify an existing one from the *Run and Debug* tab, then add the following configuration.
194+
Next, define how VSCode should connect to the remote Node.js application. Create a new `launch.json` file or modify an
195+
existing one from the *Run and Debug* tab, then add the following configuration.
183196
184197
```json
185198
{
186-
"version": "0.2.0",
187-
"configurations": [
188-
{
189-
"address": "127.0.0.1",
190-
"localRoot": "${workspaceFolder}",
191-
"name": "Attach to Remote Node.js",
192-
"port": 9229,
193-
"remoteRoot": "/var/task/",
194-
"request": "attach",
195-
"type": "node",
196-
"preLaunchTask": "Wait Remote Debugger Server"
197-
},
198-
]
199-
}
199+
"version": "0.2.0",
200+
"configurations": [
201+
{
202+
"address": "127.0.0.1",
203+
"localRoot": "${workspaceFolder}",
204+
"name": "Attach to Remote Node.js",
205+
"port": 9229,
206+
"remoteRoot": "/var/task/",
207+
"request": "attach",
208+
"type": "node",
209+
"preLaunchTask": "Wait Remote Debugger Server"
210+
}
211+
]
212+
}
200213
```
201214
202215
4️⃣ **Running the Debugger** 🏃
203216
204-
Finally, run the debugger by selecting the *Attach to Remote Node.js* configuration from the *Run and Debug* tab. You can now set breakpoints and debug your Node.js application running in a Docker container. 🐳
217+
Finally, run the debugger by selecting the *Attach to Remote Node.js* configuration from the *Run and Debug* tab. You
218+
can now set breakpoints and debug your Node.js application running in a Docker container. 🐳
205219
206220
## 📚 Resources 📚
207221

devops-tooling/docker-compose.yml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
version: "3.9"
2+
services:
3+
localstack:
4+
container_name: "${APP_NAME}-localstack"
5+
image: localstack/localstack-pro:latest
6+
ports:
7+
- "4566:4566" # LocalStack Gateway
8+
- "4510-4560:4510-4560" # external services port range
9+
- "8001:8080" # only required for Pro
10+
- "443:443" # LocalStack HTTPS Gateway (required for Pro)
11+
- "4571:4571" # elasticsearch service
12+
environment:
13+
- DEBUG=1 # turn debug output on
14+
- ENFORCE_IAM=${ENFORCE_IAM-1} # Enable IAM policy evaluation and enforcement
15+
- LAMBDA_RUNTIME_ENVIRONMENT_TIMEOUT=90
16+
- PERSIST_ALL=${PERSIST_ALL-false}
17+
- LOCALSTACK_API_KEY=${LOCALSTACK_API_KEY-}
18+
- LOCALSTACK_AUTH_TOKEN=${LOCALSTACK_AUTH_TOKEN-}
19+
volumes:
20+
- /var/run/docker.sock:/var/run/docker.sock # Mount the Docker socket into the container

iac/awscdk/bin/repo.js

Lines changed: 0 additions & 21 deletions
This file was deleted.

iac/awscdk/bin/repo.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#!/usr/bin/env node
2+
3+
import * as cdk from 'aws-cdk-lib'
4+
import {RepoStack} from '../lib/repo-stack'
5+
6+
const app = new cdk.App()
7+
new RepoStack(app, 'RepoStack', {
8+
/* If you don't specify 'env', this stack will be environment-agnostic.
9+
* Account/Region-dependent features and context lookups will not work,
10+
* but a single synthesized template can be deployed anywhere. */
11+
12+
/* Uncomment the next line to specialize this stack for the AWS Account
13+
* and Region that are implied by the current CLI configuration. */
14+
// env: { account: process.env.CDK_DEFAULT_ACCOUNT, region: process.env.CDK_DEFAULT_REGION },
15+
16+
/* Uncomment the next line if you know exactly what Account and Region you
17+
* want to deploy the stack to. */
18+
// env: { account: '123456789012', region: 'us-east-1' },
19+
20+
/* For more information, see https://docs.aws.amazon.com/cdk/latest/guide/environments.html */
21+
})

0 commit comments

Comments
 (0)