From b8be2da47691d571e10235a4c388b34b2316f46e Mon Sep 17 00:00:00 2001 From: Josh Worden Date: Wed, 10 Jul 2024 10:38:19 -0500 Subject: [PATCH] Import to CFN, add deployment docs --- .cloudformation/stack.yaml | 120 +++++++++++++++++++++++++++++++++++++ README.md | 14 +++++ scripts/cfn.sh | 43 +++++++++++++ 3 files changed, 177 insertions(+) create mode 100644 .cloudformation/stack.yaml create mode 100755 scripts/cfn.sh diff --git a/.cloudformation/stack.yaml b/.cloudformation/stack.yaml new file mode 100644 index 0000000..dda7083 --- /dev/null +++ b/.cloudformation/stack.yaml @@ -0,0 +1,120 @@ +--- +Metadata: + TemplateId: "arn:aws:cloudformation:us-east-2:598984531759:generatedTemplate/72c5ae22-f4c8-45ee-8196-f8f270bdbd5a" +Parameters: + ZipHash: + Type: "String" + Description: "The MD5 hash of the code archive to be deployed" + MongoURL: + NoEcho: "true" + Type: "String" + Description: "The MongoDB connection string for P1Events/Scorpia" +Resources: + EventsRule00rulemostpopularweeklycontent00tQtMF: + UpdateReplacePolicy: "Retain" + Type: "AWS::Events::Rule" + DeletionPolicy: "Retain" + Properties: + EventBusName: "default" + ScheduleExpression: "rate(30 minutes)" + Targets: + - Arn: + Fn::GetAtt: + - "LambdaFunction00mostpopularweeklycontent00YcIFv" + - "Arn" + Id: "Idae044b04-3016-4113-b113-3308a135cfa5" + Id: "most-popular-weekly-content" + State: "ENABLED" + Name: "most-popular-weekly-content" + LambdaPermission00functionmostpopularweeklycontent00iEG4N: + UpdateReplacePolicy: "Retain" + Type: "AWS::Lambda::Permission" + DeletionPolicy: "Retain" + Properties: + FunctionName: + Fn::GetAtt: + - "LambdaFunction00mostpopularweeklycontent00YcIFv" + - "Arn" + Action: "lambda:InvokeFunction" + SourceArn: + Ref: "EventsRule00rulemostpopularweeklycontent00tQtMF" + Principal: "events.amazonaws.com" + IAMManagedPolicy00policyserviceroleAWSLambdaTracerAccessExecutionRolee8b2fd31ff6c40aca5f006ca6974de2c00N0j5K: + UpdateReplacePolicy: "Retain" + Type: "AWS::IAM::ManagedPolicy" + DeletionPolicy: "Retain" + Properties: + ManagedPolicyName: "AWSLambdaTracerAccessExecutionRole-e8b2fd31-ff6c-40ac-a5f0-06ca6974de2c" + Path: "/service-role/" + Description: "" + Groups: [] + PolicyDocument: + Version: "2012-10-17" + Statement: + Resource: + - "*" + Action: + - "xray:PutTraceSegments" + - "xray:PutTelemetryRecords" + Effect: "Allow" + Roles: + - "website-events" + Users: [] + SQSQueue00mostpopularweeklycontentdlq00CRPbe: + UpdateReplacePolicy: "Retain" + Type: "AWS::SQS::Queue" + DeletionPolicy: "Retain" + Properties: + SqsManagedSseEnabled: false + ReceiveMessageWaitTimeSeconds: 0 + DelaySeconds: 0 + MessageRetentionPeriod: 1209600 + MaximumMessageSize: 262144 + VisibilityTimeout: 30 + QueueName: "most-popular-weekly-content-dlq" + LambdaFunction00mostpopularweeklycontent00YcIFv: + UpdateReplacePolicy: "Retain" + Type: "AWS::Lambda::Function" + DeletionPolicy: "Retain" + Properties: + MemorySize: 256 + Description: "" + TracingConfig: + Mode: "PassThrough" + VpcConfig: + SecurityGroupIds: + - "sg-08fe201a62edb4ee1" + SubnetIds: + - "subnet-0def7e10df98dbf02" + - "subnet-0f072553096e54b16" + - "subnet-03ea88b9eb475e4b5" + Ipv6AllowedForDualStack: false + DeadLetterConfig: + TargetArn: + Fn::GetAtt: + - "SQSQueue00mostpopularweeklycontentdlq00CRPbe" + - "Arn" + Timeout: 600 + RuntimeManagementConfig: + UpdateRuntimeOn: "Auto" + Handler: "packages/lambda/handlers/generate-content.handler" + Code: + S3Bucket: "cf-templates-most-popular-content" + S3Key: + Fn::Sub: "lambda/most-popular-content.${ZipHash}.zip" + Role: "arn:aws:iam::598984531759:role/service-role/website-events" + FileSystemConfigs: [] + FunctionName: "most-popular-weekly-content" + Runtime: "nodejs14.x" + PackageType: "Zip" + LoggingConfig: + LogFormat: "Text" + LogGroup: "/aws/lambda/most-popular-weekly-content" + Environment: + Variables: + MONGO_URL: + Ref: "MongoURL" + EphemeralStorage: + Size: 512 + Architectures: + - "x86_64" diff --git a/README.md b/README.md index 060f413..b418783 100644 --- a/README.md +++ b/README.md @@ -1 +1,15 @@ # Most Popular Website Content + +## API Deployment + +The API project is built and deployed to K8S via Github Actions + +## Lambda Deployment + +This project has been imported to a [AWS Cloudformation](https://us-east-2.console.aws.amazon.com/cloudformation/home?region=us-east-2#/stacks/stackinfo?stackId=arn%3Aaws%3Acloudformation%3Aus-east-2%3A598984531759%3Astack%2Fmost-popular-content%2Ffd7e5790-3ed0-11ef-a333-021e135c1eab) stack, and changes _must_ be made via the +[scripts/cfn.sh](.scripts/cfn.sh) script. You must have valid AWS access keys with +permission to modify the Lambda function. + +```sh +./scripts/cfn.sh +``` diff --git a/scripts/cfn.sh b/scripts/cfn.sh new file mode 100755 index 0000000..c5bae10 --- /dev/null +++ b/scripts/cfn.sh @@ -0,0 +1,43 @@ +#!/bin/bash +set -e +export $(grep -v '^#' .env | xargs) + +usage() { + printf "\nUsage: $0 \n" + printf "This script requires the AWS CLI with an active profile configured!\n" + printf "The \$MONGO_URL environment variable must be set!\n" + exit 1 +} +[[ -z "$MONGO_URL" ]] && usage + +updateTemplates() { + mkdir -p dist/lambda + aws s3 sync .cloudformation s3://cf-templates-most-popular-content/templates + + echo "Building lambda functions" + ./scripts/build-lambdas.js + + ZIP_HASH=$(md5 -q dist/lambda.zip) + + echo "Uploading lambda functions" + aws s3 cp dist/lambda.zip s3://cf-templates-most-popular-content/lambda/most-popular-content.$ZIP_HASH.zip +} + +updateStack() { + echo "Updating most-popular-content CloudFormation stack..." + aws cloudformation update-stack \ + --stack-name most-popular-content \ + --region us-east-2 \ + --parameters ParameterKey=MongoURL,ParameterValue=$MONGO_URL \ + ParameterKey=ZipHash,ParameterValue=$ZIP_HASH \ + --template-url https://s3.us-east-2.amazonaws.com/cf-templates-most-popular-content/templates/stack.yaml \ + --capabilities CAPABILITY_NAMED_IAM + echo "Waiting for the stack to be updated, this may take a few minutes..." + echo "See the progress at: https://us-east-2.console.aws.amazon.com/cloudformation/home?region=us-east-2#/stacks" + aws cloudformation wait stack-update-complete \ + --stack-name most-popular-content \ + --region us-east-2 +} + +updateTemplates +updateStack