Creates infrastructure for uploads to s3 and sending them to tree-tracker-web. Also has the Lambda function code.
- When a file is uploaded to
/unituploads
in the bucket,- Lambda function checks if this item has been processed
- It checks if a file with the same name, but
unituploads
replaced withprocessed
and with a.json
file ending added on. If so, it stops running.
- It checks if a file with the same name, but
- Gets the meta data from the s3 object
- Sends that to tree tracker API
- Creates a JSON file with the same name, but
unituploads
replaced withprocessed
and with a.json
file ending added on.- This file contains the metadata.
- That way if it doesn't end up saved in the tree tracker, we have it.
- Lambda function checks if this item has been processed
- If you change any of the infrastruture, or the trigger function:
- If you commit the change to the staging branch, the staging environment will update.
- If you commit the chnage to the main branch, the production environment will update.
The Lambda function writes logs to Cloudwatch
- Login to AWS console.
- Go to Cloudwatch and click "Log Groups"
- Find the log group for the lambda
/aws/lambda/ImageServiceStackBucketPutHandler
/aws/lambda/ImageServiceStackStagingBucketPutHandler
- https://docs.aws.amazon.com/AmazonCloudWatch/latest/logs/Working-with-log-groups-and-streams.html
- Every time there is a deployment or the function scales to zero, a new log stream is created.
- You can search across all streams in group with the "search all log streams" button on the right.
The following environment variables are used in the image-service.ts
file:
Variable Name | Description |
---|---|
ACCOUNT_ID |
The AWS account ID to deploy the stack to. If not set, the default value is '163934172718' . |
AWS_REGION |
The AWS region to deploy the stack to. If not set, the default value is 'eu-west-2' . |
BUCKET_NAME |
The name of the S3 bucket to create for production images. If not set, the default value is 'protect-earth-prod-photos-58374' . |
BUCKET_NAME_STAGING |
The name of the S3 bucket to create for staging images. |
ERRORS_EMAIL |
The email address to send error notifications to. |
TREE_API_URL |
The URL of the Tree API to use. |
TREE_API_URL_STAGING |
The URL of the staging tree API to use |
TREE_API_TOKEN |
The bearer token for production |
TREE_API_TOKEN_STAGING |
The bearer token for staging |
These environment variables are used to configure the ImageService
stack.
Note: TREE_API_URL
and TREE_API_URL_STAGING
are the URL for the application, the /uploads endpoint will be used.
Deployment is automated in Github actions.
- Deployg to prod:
npm run deploy:prod
- Deploy staging
npm run deploy:staging
- If you have a
protect_earth
profile in~/.aws/config
--profile=protect_earth
- Push to main branch
- Deploys to prod
- Push to any other branch
- Deploys to staging.
Deployment is automated in Github actions. These commands asume you have protect_earth
profile in ~/.aws/config
. Please update as needed.
- Deploy to prod:
npm run deploy:prod -- --profile=protect_earth
- Deploy staging
npm run deploy:staging -- --profile=protect_earth
- Push to main branch
- Deploys to prod
- Push to any other branch
- Deploys to staging.
Before beginging, make sure these assumptions are true:
- You have AWS CDK toolkit installed.
- You haved Bootstrapped CDK for the account and region you will be deploying to.
- You have a named profile setup for AWS CLI for this Github account
- Deploy the first time locally.
- Setup .env file locally with the .env variables for this client
- Deploy to staging
npm run cdk deploy cdk deploy ImageServiceStackStaging --profile=client-aws-profile
- Deploy prod as well, if stage deploy worked:
npm run cdk deploy cdk deploy ImageServiceStack --profile=client-aws-profile
- Setup automated deployments
- @todo
- In your AWS account there should be IAM users with the names
iOsAppUserImageServiceStack
andiOsAppUserImageServiceStackStaging
- Create and download an access key for those users, they are needed in the iOS app.