AWS notes / best practices
Gotchas
Ref vs GetAtt
- Table listing when to use Ref vs Fn::GetAtt: https://theburningmonk.com/cloudformation-ref-and-getatt-cheatsheet/
- Another tip by @rchrdbyd: "ALWAYS USE GetAtt!"
S3
- Cross-account S3 access, give the other account permissions to access the bucket.
CDK
CDK is written in TypeScript and run in NodeJS. All other supported languages use this backend and toolset.
Lambda
- Typically a file called bootstrap that invokes the runtime. The runtime:
- Calls some APIs to get event data and create a context
- Invokes the function, which is your main business logic
-
Calls another API to provide the response
-
Here's an example of custom runtime (via):
#!/bin/sh
set -euo pipefail
# Initialization - load function handler
source $LAMBDA_TASK_ROOT/"$(echo $_HANDLER | cut -d. -f1).sh"
# Processing
while true
do
HEADERS="$(mktemp)"
# Get an event. The HTTP request will block until one is received
EVENT_DATA=$(curl -sS -LD "$HEADERS" -X GET "http://${AWS_LAMBDA_RUNTIME_API}/2018-06-01/runtime/invocation/next")
# Extract request ID by scraping response headers received above
REQUEST_ID=$(grep -Fi Lambda-Runtime-Aws-Request-Id "$HEADERS" | tr -d '[:space:]' | cut -d: -f2)
# Run the handler function from the script
RESPONSE=$($(echo "$_HANDLER" | cut -d. -f2) "$EVENT_DATA")
# Send the response
curl -X POST "http://${AWS_LAMBDA_RUNTIME_API}/2018-06-01/runtime/invocation/$REQUEST_ID/response" -d "$RESPONSE"
done
CodeDeploy
- Pipeline connects artifacts with source
Quickstart
cdk init \ # initializes a CDK project
--app APP_NAME \ # Optional. Uses name of parent folder for stack names.
--language [typescript|python|javascript|c#|java]
npm run [build|watch] # Builds the TS files and compiles into JavaScript
cdk synth # Compiles CDK into Cloudformation
cdk deploy # deploys