|
6 | 6 | [](https://github.com/rnag/py-secrets-cache/actions/workflows/release.yml) |
7 | 7 | [](https://secrets-cache.readthedocs.io/en/latest/?version=latest) |
8 | 8 |
|
| 9 | +> [!tip] Want a full AWS Lambda + CDK deployment example? |
| 10 | +> Check out [secrets-cache-cdk-example](https://github.com/rnag/secrets-cache-cdk-example) |
| 11 | +> for a ready-to-deploy Python CDK project that demonstrates `secrets-cache` usage |
| 12 | +> with Secrets Manager and SSM parameters, including caching timings. |
| 13 | +
|
9 | 14 | Cache secrets locally from AWS Secrets Manager and other secret stores, with optional local caching for development or Lambda-friendly usage. |
10 | 15 |
|
11 | 16 | * PyPI package: https://pypi.org/project/secrets-cache/ |
@@ -116,6 +121,68 @@ pip install secrets-cache[local] |
116 | 121 |
|
117 | 122 | This enables optional `~/.secrets_cache.toml` caching for local testing. |
118 | 123 |
|
| 124 | +## AWS CDK Example |
| 125 | + |
| 126 | +We’ve created a small **AWS CDK Python project** that demonstrates how to use `secrets-cache` in an AWS Lambda function. |
| 127 | + |
| 128 | +**Repository:** [secrets-cache-cdk-example](https://github.com/rnag/secrets-cache-cdk-example) |
| 129 | + |
| 130 | +This example shows: |
| 131 | + |
| 132 | +* How to deploy a Lambda function using CDK that automatically installs `secrets-cache`. |
| 133 | +* How to fetch **Secrets Manager secrets** and **SSM parameters** from Lambda. |
| 134 | +* How module-level caching in `secrets-cache` speeds up repeated fetches in warm Lambda containers. |
| 135 | +* How to log fetch times in milliseconds to observe caching in action. |
| 136 | + |
| 137 | +### Quickstart |
| 138 | + |
| 139 | +1. Sign up for an [AWS account](https://aws.amazon.com/free/) (free tier is sufficient). |
| 140 | +2. Install the [AWS CLI](https://aws.amazon.com/cli/) and run: |
| 141 | + |
| 142 | +```bash |
| 143 | +aws configure |
| 144 | +``` |
| 145 | + |
| 146 | +3. Install [Docker Desktop](https://www.docker.com/products/docker-desktop) (needed for CDK bundling). |
| 147 | +4. Clone the example repo: |
| 148 | + |
| 149 | +```bash |
| 150 | +git clone https://github.com/rnag/secrets-cache-cdk-example |
| 151 | +cd secrets-cache-cdk-example |
| 152 | +``` |
| 153 | + |
| 154 | +5. Install dependencies and activate the virtual environment: |
| 155 | + |
| 156 | +```bash |
| 157 | +python3 -m venv .venv |
| 158 | +source .venv/bin/activate |
| 159 | +pip install -r requirements.txt |
| 160 | +``` |
| 161 | + |
| 162 | +6. If this is your first CDK deployment in the account, bootstrap it: |
| 163 | + |
| 164 | +```bash |
| 165 | +cdk bootstrap |
| 166 | +``` |
| 167 | + |
| 168 | +7. Deploy the stack: |
| 169 | + |
| 170 | +```bash |
| 171 | +cdk deploy |
| 172 | +``` |
| 173 | + |
| 174 | +8. Invoke the Lambda and see **timings for secret/parameter fetches** in real time: |
| 175 | + |
| 176 | +```bash |
| 177 | +aws lambda invoke \ |
| 178 | + --function-name CdkExampleStack-TestLambda \ |
| 179 | + --log-type Tail \ |
| 180 | + --query 'LogResult' \ |
| 181 | + --output text | base64 --decode |
| 182 | +``` |
| 183 | + |
| 184 | +Logs will show how fast the secret and parameter are fetched, demonstrating caching between warm starts. |
| 185 | + |
119 | 186 | ## Credits |
120 | 187 |
|
121 | 188 | Created with [Cookiecutter](https://github.com/audreyfeldroy/cookiecutter) and the [rnag/cookiecutter-pypackage](https://github.com/rnag/cookiecutter-pypackage) template. |
0 commit comments