Skip to content

Commit ac2bcf8

Browse files
committed
First draft
1 parent 3860739 commit ac2bcf8

File tree

1 file changed

+153
-12
lines changed

1 file changed

+153
-12
lines changed

WORKSHOP.md

Lines changed: 153 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,26 +4,167 @@ Workshop description.
44

55
---
66

7-
## 1. Section one
7+
## 1. Project setup and basic build
88

9-
1. **Step one**
10-
Instructions.
9+
1. **Create project folder**
10+
Create a new folder for your project in a sensible location, for example:
1111

12-
```text
13-
Any command that needs copying
12+
```shell
13+
mkdir -p ~/Documents/daemon-labs/docker-aws-lambda
1414
```
1515

16-
> Any notes.
16+
> You can either create this via a terminal window or your file explorer.
17+
18+
2. **Open the new folder in your code editor**
19+
20+
> If you are using VSCode, we can now do everything from within the code editor.
21+
22+
3. **Create `Dockerfile`**
23+
Add the following content:
24+
25+
```Dockerfile
26+
FROM public.ecr.aws/lambda/nodejs:22
27+
```
28+
29+
4. **Create `docker-compose.yaml`**
30+
Add the following content to define your service:
31+
32+
```yaml
33+
---
34+
services:
35+
lambda:
36+
build: .
37+
```
38+
39+
> You'll also notice that we're mounting a volume, this is to ensure any generated files are saved back to your local host folder.
40+
41+
5. **Initial image check**
42+
- Run the following command
43+
44+
```shell
45+
docker compose build
46+
```
47+
48+
> If you now run `docker images`, you'll see a newly created image which should be around 226MB in size.
49+
50+
- Run the following command
51+
52+
```shell
53+
docker compose run -it --rm --entrypoint /bin/sh -v ./app:/var/task lambda
54+
```
55+
56+
> This command opens an interactive session with the container.
57+
58+
- Run the following command
59+
60+
```shell
61+
node --version
62+
```
63+
64+
> The output should start with `v22` followed by the latest minor and patch version.
1765

1866
---
1967

20-
## 2. Section two
68+
## 2. Dependency management and TypeScript config
69+
70+
1. **Initialise project and install dev dependencies**
71+
- Run the following command
72+
73+
```shell
74+
npm init -y
75+
```
76+
77+
> Notice how the `lambda` directory is automatically created on your host machine due to the volume mount.
78+
79+
- Run the following command
80+
81+
```shell
82+
npm add --save-dev @types/node@22 @types/aws-lambda @tsconfig/recommended typescript
83+
```
2184

22-
1. **Step one**
23-
Instructions.
85+
> Notice this automatically creates a `package-lock.json` file.
86+
> Even though dependencies have been installed, if you run `docker images` again, you'll see the image size hasn't changed because the `node_modules` were written to your local volume, not the image layer.
2487

25-
```text
26-
Any command that needs copying
88+
3. **Exit the container**
89+
- Run the following command
90+
91+
```shell
92+
exit
93+
```
94+
95+
> We are now done with the interactive container at this stage and no longer need it.
96+
97+
4. **Create `tsconfig.json`**
98+
Create `tsconfig.json` and add the following content to configure the TypeScript compiler:
99+
100+
```json
101+
{
102+
"extends": "@tsconfig/recommended/tsconfig.json",
103+
"compilerOptions": {
104+
"outDir": "./build/dist"
105+
}
106+
}
27107
```
28108

29-
> Any notes.
109+
> ℹ️ While you could auto-generate this file, our manual configuration using a recommended preset keeps the file minimal and clean.
110+
111+
5. **Create source file and scripts**
112+
- Create `./src/index.ts` with the following:
113+
114+
```typescript
115+
import { Handler } from "aws-lambda";
116+
117+
export const handler: Handler = (event,context) => {
118+
console.log("Hello world!");
119+
};
120+
```
121+
122+
- Add the following to the `scripts` section in your `package.json`:
123+
124+
```json
125+
"build": "npm run build:tsc && npm run build:dependencies",
126+
"build:tsc": "rm -rf ./build/dist && tsc",
127+
"build:dependencies": "rm -rf ./build/dependencies && mkdir -p ./build/dependencies/nodejs && cp ./package*.json ./build/dependencies/nodejs && npm ci --omit dev --prefix ./build/dependencies/nodejs",
128+
```
129+
130+
Update the `Dockerfile`
131+
132+
```Dockerfile
133+
FROM public.ecr.aws/lambda/nodejs:22
134+
135+
COPY ./app /var/task
136+
137+
RUN npm ci && npm run build
138+
139+
HEALTHCHECK --interval=1s --timeout=1s --retries=30 \
140+
CMD [ "curl", "-I", "http://localhost:8080" ]
141+
142+
CMD [ "build/dist/index.handler" ]
143+
```
144+
145+
Run `docker compose up --build`
146+
> This Lambda starts but nothing happens
147+
148+
Kill the container `Ctrl+C`
149+
150+
Add a new service to the `docker-compos.yaml` file
151+
152+
```yaml
153+
curl:
154+
image: curlimages/curl
155+
depends_on:
156+
lambda:
157+
condition: service_healthy
158+
command:
159+
- -s
160+
- -d {}
161+
- http://lambda:8080/2015-03-31/functions/function/invocations
162+
```
163+
164+
Run `docker compose up --build`
165+
> The Lambda and cURL containers start and execute, the cURL container responded with an exite code of 0 but is still hanging
166+
167+
Kill the container `Ctrl+C`
168+
169+
Run `docker compose up --build --abort-on-container-exit`
170+
> The Lambda and cURL containers start and execute, the cURL container responded with an exite code of 0 and both containers shut down

0 commit comments

Comments
 (0)