|
| 1 | +# API Gateway Resource Policy Update Script |
| 2 | + |
| 3 | +This script updates your API Gateway resource policy after your CDK deployment to allow only Cognito authenticated users and deny non-HTTPS requests. Using a separate script avoids the circular dependency issues that can occur when adding policies in CDK. |
| 4 | + |
| 5 | +## Setup |
| 6 | + |
| 7 | +1. Save the script to a file named `update-api-policy.js` in your project. |
| 8 | + |
| 9 | +2. Install the AWS SDK if you haven't already: |
| 10 | + ```bash |
| 11 | + npm install aws-sdk |
| 12 | + ``` |
| 13 | + |
| 14 | +3. Make the script executable: |
| 15 | + ```bash |
| 16 | + chmod +x update-api-policy.js |
| 17 | + ``` |
| 18 | + |
| 19 | +## Configuration |
| 20 | + |
| 21 | +Update the following variables in the script to match your environment: |
| 22 | + |
| 23 | +- `STACK_NAME`: The name of your CloudFormation stack (e.g., 'ai-team-medical-reports-stack-development') |
| 24 | +- `REGION`: Your AWS region (e.g., 'us-east-1') |
| 25 | +- `API_NAME`: The name of your API Gateway (e.g., 'AIMedicalReport-development') |
| 26 | + |
| 27 | +## Usage |
| 28 | + |
| 29 | +You can run the script after each successful CDK deployment: |
| 30 | + |
| 31 | +```bash |
| 32 | +# Run CDK deployment first |
| 33 | +cdk deploy ai-team-medical-reports-stack-development |
| 34 | + |
| 35 | +# Then run the policy update script |
| 36 | +./update-api-policy.js |
| 37 | +``` |
| 38 | + |
| 39 | +Alternatively, you can set the Cognito User Pool ID as an environment variable: |
| 40 | + |
| 41 | +```bash |
| 42 | +COGNITO_USER_POOL_ID=us-east-1_yourPoolId ./update-api-policy.js |
| 43 | +``` |
| 44 | + |
| 45 | +## Automation |
| 46 | + |
| 47 | +To automatically run this after each deployment, you can create a simple shell script: |
| 48 | + |
| 49 | +```bash |
| 50 | +#!/bin/bash |
| 51 | +# deploy-and-update.sh |
| 52 | + |
| 53 | +# Deploy with CDK |
| 54 | +cdk deploy ai-team-medical-reports-stack-development |
| 55 | + |
| 56 | +# If deployment was successful, update the API policy |
| 57 | +if [ $? -eq 0 ]; then |
| 58 | + echo "CDK deployment successful, updating API policy..." |
| 59 | + ./update-api-policy.js |
| 60 | +else |
| 61 | + echo "CDK deployment failed, skipping API policy update." |
| 62 | + exit 1 |
| 63 | +fi |
| 64 | +``` |
| 65 | + |
| 66 | +Make it executable: |
| 67 | +```bash |
| 68 | +chmod +x deploy-and-update.sh |
| 69 | +``` |
| 70 | + |
| 71 | +## Troubleshooting |
| 72 | + |
| 73 | +If you encounter any issues: |
| 74 | + |
| 75 | +1. **Authentication errors**: Make sure your AWS credentials are configured correctly with the necessary permissions. |
| 76 | + |
| 77 | +2. **API not found**: Verify the API_NAME matches exactly what's in your AWS Console. |
| 78 | + |
| 79 | +3. **Stack not found**: Check that the STACK_NAME is correct. |
| 80 | + |
| 81 | +4. **Cognito User Pool ID not found**: You can set it manually with the COGNITO_USER_POOL_ID environment variable. |
| 82 | + |
| 83 | +## Security Considerations |
| 84 | + |
| 85 | +This script sets a resource policy that: |
| 86 | + |
| 87 | +1. Allows only authenticated Cognito users to access your API |
| 88 | +2. Denies any non-HTTPS requests to your API |
| 89 | + |
| 90 | +If you need more complex permissions, you can modify the policy object in the script. |
0 commit comments