SQL Writer
This guide shows you how to build a hybrid cloud scenario using LocalStack and an external PostgreSQL instance. This lab demonstrates how an AWS Lambda retrieves secrets from Secrets Manager and interacts with a database outside the AWS environment.
Prerequisites
Section titled “Prerequisites”- Docker installed and running.
- Dev Containers extension installed (optional).
- Terraform (included in Dev Container).
- AWS CLI (included in Dev Container).
- DBeaver or any SQL client (to verify results).
How to execute
Section titled “How to execute”-
Setup Environment:
Open VS Code in the project folder and execute this command in the Command Palette:
Terminal window > Dev Containers: Reopen in ContainerTerminal window docker compose up -d -
Then configure the AWS CLI:
Terminal window aws configure set aws_access_key_id testaws configure set aws_secret_access_key testaws configure set region us-east-1 -
Deploy Resources:
Terminal window python package_lambda.pyterraform init && terraform apply -auto-approve -
Run the Example:
Terminal window aws --endpoint-url=http://localhost:4566 lambda invoke \--function-name AddUserFunction \--payload '{}' \response.json
Validate results
Section titled “Validate results”-
Check using DBeaver: Connect to the local PostgreSQL instance and run the following query:
- Host:
localhost. - Port:
5432. - Database:
mydb. - User:
myuser. - Password:
mypassword.
SELECT * FROM users ORDER BY created_at DESC; - Host:
-
Check using AWS CLI:
- List Lambda Functions:
Terminal window aws --endpoint-url=http://localhost:4566 lambda list-functions - Check Secrets:
Terminal window aws --endpoint-url=http://localhost:4566 secretsmanager list-secrets
- List Lambda Functions:
Clean Up
Section titled “Clean Up”terraform destroy -auto-approvedocker compose down -vrm lambda.zip response.jsonTroubleshooting
Section titled “Troubleshooting”| Issue | Solution |
|---|---|
| Connection Refused | Ensure host.docker.internal is reachable from the LocalStack container. For older Docker on Linux, add it to extra_hosts in docker-compose.yml. |
| Port Already in Use | Modify the port mappings for 4566 or 5432 in docker-compose.yml. |