Skip to content

Commit

Permalink
Try adding the lambda to dev
Browse files Browse the repository at this point in the history
  • Loading branch information
Liam Lloyd committed Aug 28, 2024
1 parent 655993f commit d6e9ec9
Show file tree
Hide file tree
Showing 10 changed files with 335 additions and 52 deletions.
38 changes: 34 additions & 4 deletions .github/workflows/dev_deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,31 @@ jobs:
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
- name: Publish Image to ECR
run: docker push $AM_CLEANUP_IMAGE_TAG
build_record_thumbnail_lambda:
needs:
- run_tests
runs-on: ubuntu-20.04
steps:
- uses: actions/checkout@v3
- name: Generate Image Tag
run: echo "RECORD_THUMBNAIL_LAMBDA_IMAGE_TAG=364159549467.dkr.ecr.$AWS_REGION.amazonaws.com/stela:record_thumbnail-$([[ ${GITHUB_REF##*/} = main ]] && echo main || echo feature)-$(git rev-parse --short $GITHUB_SHA)" >> $GITHUB_ENV
env:
AWS_REGION: ${{ secrets.AWS_DEFAULT_REGION }}
- name: Build Image
run: docker build -t $RECORD_THUMBNAIL_LAMBDA_IMAGE_TAG -f Dockerfile.record_thumbnail_attacher .
- name: AWS Login
run: aws ecr get-login-password --region $AWS_REGION | docker login --username AWS --password-stdin 364159549467.dkr.ecr.$AWS_REGION.amazonaws.com
env:
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
AWS_REGION: ${{ secrets.AWS_DEFAULT_REGION }}
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
- name: Publish Image to ECR
run: docker push $RECORD_THUMBNAIL_LAMBDA_IMAGE_TAG
deploy:
needs:
- build_api
- build_am_cleanup
- build_record_thumbnail_lambda
runs-on: ubuntu-20.04
defaults:
run:
Expand All @@ -73,6 +94,10 @@ jobs:
run: echo "AM_CLEANUP_IMAGE_TAG=364159549467.dkr.ecr.$AWS_REGION.amazonaws.com/stela:am_cleanup-$([[ ${GITHUB_REF##*/} = main ]] && echo main || echo feature)-$(git rev-parse --short $GITHUB_SHA)" >> $GITHUB_ENV
env:
AWS_REGION: ${{ secrets.AWS_DEFAULT_REGION }}
- name: Generate Record Thumbnail Image Tag
run: echo "RECORD_THUMBNAIL_LAMBDA_IMAGE_TAG=364159549467.dkr.ecr.$AWS_REGION.amazonaws.com/stela:record_thumbnail-$([[ ${GITHUB_REF##*/} = main ]] && echo main || echo feature)-$(git rev-parse --short $GITHUB_SHA)" >> $GITHUB_ENV
env:
AWS_REGION: ${{ secrets.AWS_DEFAULT_REGION }}
- name: Terraform Init
id: init
run: terraform init
Expand All @@ -83,11 +108,16 @@ jobs:
# the -target option restricts terraform to just updating the dev deployment
- name: Terraform Plan for API
id: plan_api
run: terraform plan -no-color -input=false -var="stela_dev_image=$API_IMAGE_TAG" -var="stela_staging_image=$API_IMAGE_TAG" -var="archivematica_cleanup_dev_image=$AM_CLEANUP_IMAGE_TAG" -var="archivematica_cleanup_staging_image=$AM_CLEANUP_IMAGE_TAG" -target=kubernetes_deployment.stela_dev
run: terraform plan -no-color -input=false -var="stela_dev_image=$API_IMAGE_TAG" -var="stela_staging_image=$API_IMAGE_TAG" -var="archivematica_cleanup_dev_image=$AM_CLEANUP_IMAGE_TAG" -var="archivematica_cleanup_staging_image=$AM_CLEANUP_IMAGE_TAG" -var="record_thumbnail_dev_lambda_image=$RECORD_THUMBNAIL_LAMBDA_IMAGE_TAG" -target=kubernetes_deployment.stela_dev
- name: Terraform Apply for API
run: terraform apply -auto-approve -input=false -var="stela_dev_image=$API_IMAGE_TAG" -var="stela_staging_image=$API_IMAGE_TAG" -var="archivematica_cleanup_dev_image=$AM_CLEANUP_IMAGE_TAG" -var="archivematica_cleanup_staging_image=$AM_CLEANUP_IMAGE_TAG" -target=kubernetes_deployment.stela_dev
run: terraform apply -auto-approve -input=false -var="stela_dev_image=$API_IMAGE_TAG" -var="stela_staging_image=$API_IMAGE_TAG" -var="archivematica_cleanup_dev_image=$AM_CLEANUP_IMAGE_TAG" -var="archivematica_cleanup_staging_image=$AM_CLEANUP_IMAGE_TAG" -var="record_thumbnail_dev_lambda_image=$RECORD_THUMBNAIL_LAMBDA_IMAGE_TAG" -target=kubernetes_deployment.stela_dev
- name: Terraform Plan for Archivematica cleanup
id: plan_am_cleanup
run: terraform plan -no-color -input=false -var="stela_dev_image=$API_IMAGE_TAG" -var="stela_staging_image=$API_IMAGE_TAG" -var="archivematica_cleanup_dev_image=$AM_CLEANUP_IMAGE_TAG" -var="archivematica_cleanup_staging_image=$AM_CLEANUP_IMAGE_TAG" -target=kubernetes_cron_job_v1.archivematica_cleanup_dev
run: terraform plan -no-color -input=false -var="stela_dev_image=$API_IMAGE_TAG" -var="stela_staging_image=$API_IMAGE_TAG" -var="archivematica_cleanup_dev_image=$AM_CLEANUP_IMAGE_TAG" -var="archivematica_cleanup_staging_image=$AM_CLEANUP_IMAGE_TAG" -var="record_thumbnail_dev_lambda_image=$RECORD_THUMBNAIL_LAMBDA_IMAGE_TAG" -target=kubernetes_cron_job_v1.archivematica_cleanup_dev
- name: Terraform Apply for Archivematica cleanup
run: terraform apply -auto-approve -input=false -var="stela_dev_image=$API_IMAGE_TAG" -var="stela_staging_image=$API_IMAGE_TAG" -var="archivematica_cleanup_dev_image=$AM_CLEANUP_IMAGE_TAG" -var="archivematica_cleanup_staging_image=$AM_CLEANUP_IMAGE_TAG" -target=kubernetes_cron_job_v1.archivematica_cleanup_dev
run: terraform apply -auto-approve -input=false -var="stela_dev_image=$API_IMAGE_TAG" -var="stela_staging_image=$API_IMAGE_TAG" -var="archivematica_cleanup_dev_image=$AM_CLEANUP_IMAGE_TAG" -var="archivematica_cleanup_staging_image=$AM_CLEANUP_IMAGE_TAG" -var="record_thumbnail_dev_lambda_image=$RECORD_THUMBNAIL_LAMBDA_IMAGE_TAG" -target=kubernetes_cron_job_v1.archivematica_cleanup_dev
- name: Terraform Plan for Record Thumbnail Lambda
id: plan_record_thumbnail_lambda
run: terraform plan -no-color -input=false -var="stela_dev_image=$API_IMAGE_TAG" -var="stela_staging_image=$API_IMAGE_TAG" -var="archivematica_cleanup_dev_image=$AM_CLEANUP_IMAGE_TAG" -var="archivematica_cleanup_staging_image=$AM_CLEANUP_IMAGE_TAG" -var="record_thumbnail_dev_lambda_image=$RECORD_THUMBNAIL_LAMBDA_IMAGE_TAG" -target=aws_lambda_function.record_thumbnail_lambda
- name: Terraform Apply for Record Thumbnail Lambda
run: terraform apply -auto-approve -input=false -var="stela_dev_image=$API_IMAGE_TAG" -var="stela_staging_image=$API_IMAGE_TAG" -var="archivematica_cleanup_dev_image=$AM_CLEANUP_IMAGE_TAG" -var="archivematica_cleanup_staging_image=$AM_CLEANUP_IMAGE_TAG" -var="record_thumbnail_dev_lambda_image=$RECORD_THUMBNAIL_LAMBDA_IMAGE_TAG" -target=aws_lambda_function.record_thumbnail_lambda
29 changes: 27 additions & 2 deletions .github/workflows/full_test_deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,31 @@ jobs:
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
- name: Publish Image to ECR
run: docker push $AM_CLEANUP_IMAGE_TAG
build_record_thumbnail_lambda:
needs:
- run_tests
runs-on: ubuntu-20.04
steps:
- uses: actions/checkout@v3
- name: Generate Image Tag
run: echo "RECORD_THUMBNAIL_LAMBDA_IMAGE_TAG=364159549467.dkr.ecr.$AWS_REGION.amazonaws.com/stela:record_thumbnail-$([[ ${GITHUB_REF##*/} = main ]] && echo main || echo feature)-$(git rev-parse --short $GITHUB_SHA)" >> $GITHUB_ENV
env:
AWS_REGION: ${{ secrets.AWS_DEFAULT_REGION }}
- name: Build Image
run: docker build -t $RECORD_THUMBNAIL_LAMBDA_IMAGE_TAG --build-arg="AWS_RDS_CERT_BUNDLE=$AWS_RDS_CERT_BUNDLE" -f Dockerfile.record_thumbnail_attacher .
- name: AWS Login
run: aws ecr get-login-password --region $AWS_REGION | docker login --username AWS --password-stdin 364159549467.dkr.ecr.$AWS_REGION.amazonaws.com
env:
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
AWS_REGION: ${{ secrets.AWS_DEFAULT_REGION }}
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
- name: Publish Image to ECR
run: docker push $RECORD_THUMBNAIL_LAMBDA_IMAGE_TAG
deploy:
needs:
- build_api
- build_am_cleanup
- build_record_thumbnail_lambda
runs-on: ubuntu-20.04
defaults:
run:
Expand All @@ -70,6 +91,10 @@ jobs:
run: echo "AM_CLEANUP_IMAGE_TAG=364159549467.dkr.ecr.$AWS_REGION.amazonaws.com/stela:am_cleanup-$([[ ${GITHUB_REF##*/} = main ]] && echo main || echo feature)-$(git rev-parse --short $GITHUB_SHA)" >> $GITHUB_ENV
env:
AWS_REGION: ${{ secrets.AWS_DEFAULT_REGION }}
- name: Generate Record Thumbnail Image Tag
run: echo "RECORD_THUMBNAIL_LAMBDA_IMAGE_TAG=364159549467.dkr.ecr.$AWS_REGION.amazonaws.com/stela:record_thumbnail-$([[ ${GITHUB_REF##*/} = main ]] && echo main || echo feature)-$(git rev-parse --short $GITHUB_SHA)" >> $GITHUB_ENV
env:
AWS_REGION: ${{ secrets.AWS_DEFAULT_REGION }}
- name: Terraform Init
id: init
run: terraform init
Expand All @@ -78,6 +103,6 @@ jobs:
run: terraform validate -no-color
- name: Terraform Plan
id: plan
run: terraform plan -no-color -input=false -var="stela_dev_image=$API_IMAGE_TAG" -var="stela_staging_image=$API_IMAGE_TAG" -var="archivematica_cleanup_dev_image=$AM_CLEANUP_IMAGE_TAG" -var="archivematica_cleanup_staging_image=$AM_CLEANUP_IMAGE_TAG"
run: terraform plan -no-color -input=false -var="stela_dev_image=$API_IMAGE_TAG" -var="stela_staging_image=$API_IMAGE_TAG" -var="archivematica_cleanup_dev_image=$AM_CLEANUP_IMAGE_TAG" -var="archivematica_cleanup_staging_image=$AM_CLEANUP_IMAGE_TAG" -var="record_thumbnail_dev_lambda_image=$RECORD_THUMBNAIL_LAMBDA_IMAGE_TAG"
- name: Terraform Apply
run: terraform apply -auto-approve -input=false -var="stela_dev_image=$API_IMAGE_TAG" -var="stela_staging_image=$API_IMAGE_TAG" -var="archivematica_cleanup_dev_image=$AM_CLEANUP_IMAGE_TAG" -var="archivematica_cleanup_staging_image=$AM_CLEANUP_IMAGE_TAG"
run: terraform apply -auto-approve -input=false -var="stela_dev_image=$API_IMAGE_TAG" -var="stela_staging_image=$API_IMAGE_TAG" -var="archivematica_cleanup_dev_image=$AM_CLEANUP_IMAGE_TAG" -var="archivematica_cleanup_staging_image=$AM_CLEANUP_IMAGE_TAG" -var="record_thumbnail_dev_lambda_image=$RECORD_THUMBNAIL_LAMBDA_IMAGE_TAG"
6 changes: 6 additions & 0 deletions Dockerfile.record_thumbnail_attacher
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,14 @@ RUN npm run build -ws


FROM public.ecr.aws/lambda/nodejs:18 as final

ARG AWS_RDS_CERT_BUNDLE

WORKDIR ${LAMBDA_TASK_ROOT}

RUN mkdir /etc/ca-certificates
RUN echo -e $AWS_RDS_CERT_BUNDLE > /etc/ca-certificates/rds-us-west-2-ca-bundle.pem

COPY --from=builder /usr/local/apps/stela/packages/record_thumbnail_attacher/dist ./packages/record_thumbnail_attacher/dist
COPY --from=builder /usr/local/apps/stela/packages/record_thumbnail_attacher/package.json ./packages/record_thumbnail_attacher/package.json
COPY --from=builder /usr/local/apps/stela/packages/logger/dist ./packages/logger/dist
Expand Down
1 change: 1 addition & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions packages/record_thumbnail_attacher/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
"aws-cloudfront-sign": "^3.0.2",
"dotenv": "^8.2.0",
"require-env-variable": "^3.1.2",
"pg": "^8.5.1",
"tinypg": "^7.0.0"
},
"devDependencies": {
Expand Down
94 changes: 68 additions & 26 deletions packages/record_thumbnail_attacher/src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,17 @@ describe("handler", () => {
messageId: "1",
receiptHandle: "1",
body: JSON.stringify({
s3: {
object: {
key: "_Liam/access_copies/e38e/8582/b417/430c/953d/5c7e/8040/1ae2/2_upload-cb45fa84-f0ea-4a9e-b1da-309e485a4f4a/object/710a1def-caf8-48f2-8eee-0848b4cfda10.jpg",
},
},
Message: JSON.stringify({
Records: [
{
s3: {
object: {
key: "_Liam/access_copies/e38e/8582/b417/430c/953d/5c7e/8040/1ae2/2_upload-cb45fa84-f0ea-4a9e-b1da-309e485a4f4a/object/710a1def-caf8-48f2-8eee-0848b4cfda10.jpg",
},
},
},
],
}),
}),
attributes: {
ApproximateReceiveCount: "1",
Expand Down Expand Up @@ -107,7 +113,19 @@ describe("handler", () => {
{
messageId: "1",
receiptHandle: "1",
body: JSON.stringify({ s3: { object: { key: "1" } } }),
body: JSON.stringify({
Message: JSON.stringify({
Records: [
{
s3: {
object: {
key: "_Liam/access_copies/e38e/8582/b417/430c/953d/5c7e/8040/1ae2/2_upload-cb45fa84-f0ea-4a9e-b1da-309e485a4f4a/object/710a1def-caf8-48f2-8eee-0848b4cfda10.jpg",
},
},
},
],
}),
}),
attributes: {
ApproximateReceiveCount: "1",
SentTimestamp: "1",
Expand Down Expand Up @@ -154,11 +172,17 @@ describe("handler", () => {
messageId: "1",
receiptHandle: "1",
body: JSON.stringify({
s3: {
object: {
key: "_Liam/access_copies/e38e/8582/b417/430c/953d/5c7e/8040/1ae2/2_load-cb45fa84-f0ea-4a9e-b1da-309e485a4f4a/thumbnails/710a1def-caf8-48f2-8eee-0848b4cfda10.jpg",
},
},
Message: JSON.stringify({
Records: [
{
s3: {
object: {
key: "_Liam/access_copies/e38e/8582/b417/430c/953d/5c7e/8040/1ae2/thumbnails/2_load-cb45fa84-f0ea-4a9e-b1da-309e485a4f4a/object/710a1def-caf8-48f2-8eee-0848b4cfda10.jpg",
},
},
},
],
}),
}),
attributes: {
ApproximateReceiveCount: "1",
Expand Down Expand Up @@ -197,11 +221,17 @@ describe("handler", () => {
messageId: "1",
receiptHandle: "1",
body: JSON.stringify({
s3: {
object: {
key: testKey,
},
},
Message: JSON.stringify({
Records: [
{
s3: {
object: {
key: testKey,
},
},
},
],
}),
}),
attributes: {
ApproximateReceiveCount: "1",
Expand Down Expand Up @@ -288,11 +318,17 @@ describe("handler", () => {
messageId: "1",
receiptHandle: "1",
body: JSON.stringify({
s3: {
object: {
key: testKey,
},
},
Message: JSON.stringify({
Records: [
{
s3: {
object: {
key: testKey,
},
},
},
],
}),
}),
attributes: {
ApproximateReceiveCount: "1",
Expand Down Expand Up @@ -350,11 +386,17 @@ describe("handler", () => {
messageId: "1",
receiptHandle: "1",
body: JSON.stringify({
s3: {
object: {
key: testKey,
},
},
Message: JSON.stringify({
Records: [
{
s3: {
object: {
key: testKey,
},
},
},
],
}),
}),
attributes: {
ApproximateReceiveCount: "1",
Expand Down
21 changes: 18 additions & 3 deletions packages/record_thumbnail_attacher/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,27 @@ import { getSignedUrl } from "aws-cloudfront-sign";
import * as Sentry from "@sentry/aws-serverless";
import { logger } from "@stela/logger";
import { db } from "./database";
import { validateNewDisseminationPackageJpgEvent } from "./validators";
import {
validateNewDisseminationPackageJpgEvent,
validateSqsMessage,
} from "./validators";

const getKeyFromS3Message = (message: SQSRecord): string => {
logger.info(`Received message: ${JSON.stringify(message)}`);
const { body } = message;
const parsedBody: unknown = JSON.parse(body);
if (!validateNewDisseminationPackageJpgEvent(parsedBody)) {
logger.info(`Parsed message: ${JSON.stringify(parsedBody)}`);
if (!validateSqsMessage(parsedBody)) {
logger.error(
`Invalid message body: ${JSON.stringify(validateSqsMessage.errors)}`
);
throw new Error("Invalid message body");
}
const parsedMessage: unknown = JSON.parse(parsedBody.Message);
if (
!validateNewDisseminationPackageJpgEvent(parsedMessage) ||
!parsedMessage.Records[0]
) {
logger.error(
`Invalid message body: ${JSON.stringify(
validateNewDisseminationPackageJpgEvent.errors
Expand All @@ -17,7 +32,7 @@ const getKeyFromS3Message = (message: SQSRecord): string => {
throw new Error("Invalid message body");
}

const { key } = parsedBody.s3.object;
const { key } = parsedMessage.Records[0].s3.object;
return key;
};

Expand Down
Loading

0 comments on commit d6e9ec9

Please sign in to comment.