From 09e23977f5633a416fc75e9a469ef0e703d246db Mon Sep 17 00:00:00 2001 From: ulises-jeremias Date: Sun, 5 Nov 2023 20:40:46 -0300 Subject: [PATCH] Updated example --- .../.env.example | 2 +- .../.env.local | 2 +- .../README.md | 6 ++++-- .../resource/DynamoDB.yml | 12 ++++++------ .../resource/S3.yml | 6 +++--- .../serverless.yml | 10 +++++----- .../src/helpers/env.ts | 14 +++++++------- .../src/rest/program.ts | 18 +++++++++--------- .../src/rest/serverless.yml | 6 +++--- 9 files changed, 39 insertions(+), 37 deletions(-) diff --git a/examples/serverless-localstack-with-s3-and-dynamodb/.env.example b/examples/serverless-localstack-with-s3-and-dynamodb/.env.example index 582404e..643c62b 100644 --- a/examples/serverless-localstack-with-s3-and-dynamodb/.env.example +++ b/examples/serverless-localstack-with-s3-and-dynamodb/.env.example @@ -1,4 +1,4 @@ # Deployment Configuration SERVICE_NAME=serverless-localstack-example -PROGRAMS_S3_BUCKET_NAME=serverless-localstack-bucket +S3_BUCKET_NAME=serverless-localstack-bucket diff --git a/examples/serverless-localstack-with-s3-and-dynamodb/.env.local b/examples/serverless-localstack-with-s3-and-dynamodb/.env.local index 582404e..643c62b 100644 --- a/examples/serverless-localstack-with-s3-and-dynamodb/.env.local +++ b/examples/serverless-localstack-with-s3-and-dynamodb/.env.local @@ -1,4 +1,4 @@ # Deployment Configuration SERVICE_NAME=serverless-localstack-example -PROGRAMS_S3_BUCKET_NAME=serverless-localstack-bucket +S3_BUCKET_NAME=serverless-localstack-bucket diff --git a/examples/serverless-localstack-with-s3-and-dynamodb/README.md b/examples/serverless-localstack-with-s3-and-dynamodb/README.md index 1a31a84..076b6de 100644 --- a/examples/serverless-localstack-with-s3-and-dynamodb/README.md +++ b/examples/serverless-localstack-with-s3-and-dynamodb/README.md @@ -39,14 +39,16 @@ pnpm install # Run the Local Stack using Docker Compose docker-compose up -d -# Run the application locally +# Set env variables and run the serverless offline +export AWS_ACCESS_KEY_ID="dummy-value" +export AWS_SECRET_ACCESS_KEY="dummy-value" pnpm run sls:offline ``` ## Testing locally ```sh -curl -X POST http://localhost:3000/program --header 'Content-Type': 'application/json' --data '{ +curl -X POST http://localhost:3000/ --header 'Content-Type: application/json' --data '{ "content": "console.log(\"Hello World\");", "filePath": "example/file.js" }' diff --git a/examples/serverless-localstack-with-s3-and-dynamodb/resource/DynamoDB.yml b/examples/serverless-localstack-with-s3-and-dynamodb/resource/DynamoDB.yml index 68a8a02..7bafecf 100644 --- a/examples/serverless-localstack-with-s3-and-dynamodb/resource/DynamoDB.yml +++ b/examples/serverless-localstack-with-s3-and-dynamodb/resource/DynamoDB.yml @@ -1,9 +1,9 @@ --- Resources: - ProgramsCache: + DynamoDBTable: Type: AWS::DynamoDB::Table Properties: - TableName: ${self:service}-${sls:stage}-programs-cache + TableName: ${self:service}-${sls:stage}-table AttributeDefinitions: - AttributeName: key AttributeType: S @@ -15,8 +15,8 @@ Resources: WriteCapacityUnits: 5 Outputs: - ProgramsCache: - Value: !Ref ProgramsCache - Description: DynamoDB table for programs cache + DynamoDBTable: + Value: !Ref DynamoDBTable + Description: DynamoDB table for the example Export: - Name: ${self:service}-${sls:stage}-programs-cache + Name: ${self:service}-${sls:stage}-table diff --git a/examples/serverless-localstack-with-s3-and-dynamodb/resource/S3.yml b/examples/serverless-localstack-with-s3-and-dynamodb/resource/S3.yml index 8f6477f..90b1ef2 100644 --- a/examples/serverless-localstack-with-s3-and-dynamodb/resource/S3.yml +++ b/examples/serverless-localstack-with-s3-and-dynamodb/resource/S3.yml @@ -3,11 +3,11 @@ Resources: S3Bucket: Type: AWS::S3::Bucket Properties: - BucketName: ${env:PROGRAMS_S3_BUCKET_NAME} + BucketName: ${env:S3_BUCKET_NAME} Outputs: S3BucketNameOutputKey: Value: !Ref S3Bucket - Description: S3 bucket for programs + Description: S3 bucket for the example Export: - Name: ${self:service}-${sls:stage}-programs-s3-bucket + Name: ${self:service}-${sls:stage}-s3-bucket diff --git a/examples/serverless-localstack-with-s3-and-dynamodb/serverless.yml b/examples/serverless-localstack-with-s3-and-dynamodb/serverless.yml index 0512141..c6d90d0 100644 --- a/examples/serverless-localstack-with-s3-and-dynamodb/serverless.yml +++ b/examples/serverless-localstack-with-s3-and-dynamodb/serverless.yml @@ -43,8 +43,8 @@ provider: environment: STAGE: ${sls:stage} PACKAGE_VERSION: ${file(package.json):version} - PROGRAMS_S3_BUCKET_NAME: ${env:PROGRAMS_S3_BUCKET_NAME} - PROGRAMS_CACHE_TABLE_NAME: ${self:service}-${sls:stage}-programs-cache + S3_BUCKET_NAME: ${env:S3_BUCKET_NAME} + TABLE_NAME: ${self:service}-${sls:stage}-table AWS_DYNAMODB_ENDPOINT: ${self:custom.config.${sls:stage}.AWS_DYNAMODB_ENDPOINT, ''} AWS_S3_ENDPOINT: ${self:custom.config.${sls:stage}.AWS_S3_ENDPOINT, ''} iam: @@ -54,18 +54,18 @@ provider: Action: - dynamodb:* Resource: - - 'arn:aws:dynamodb:::table/${self:service}-${sls:stage}-programs-cache' + - 'arn:aws:dynamodb:::table/${self:service}-${sls:stage}-table' - Effect: Allow Action: - s3:PutObject - s3:GetObject Resource: - - 'arn:aws:s3:::${env:PROGRAMS_S3_BUCKET_NAME}/*' + - 'arn:aws:s3:::${env:S3_BUCKET_NAME}/*' - Effect: Allow Action: - s3:PutObjects Resource: - - 'arn:aws:s3:::${env:PROGRAMS_S3_BUCKET_NAME}' + - 'arn:aws:s3:::${env:S3_BUCKET_NAME}' functions: - ${file(src/rest/serverless.yml)} diff --git a/examples/serverless-localstack-with-s3-and-dynamodb/src/helpers/env.ts b/examples/serverless-localstack-with-s3-and-dynamodb/src/helpers/env.ts index 6d10d50..6963faf 100644 --- a/examples/serverless-localstack-with-s3-and-dynamodb/src/helpers/env.ts +++ b/examples/serverless-localstack-with-s3-and-dynamodb/src/helpers/env.ts @@ -16,14 +16,14 @@ export const getAwsAccessKeyId = () => process.env.AWS_ACCESS_KEY_ID; // getAwsSecretAccessKey is used to determine the secret access key of the application export const getAwsSecretAccessKey = () => process.env.AWS_SECRET_ACCESS_KEY; -// getAwsLambdaEndpoint is used to determine the lambda endpoint of the application +// getAwsS3Endpoint is used to determine the s3 endpoint of the application export const getAwsS3Endpoint = () => process.env.AWS_S3_ENDPOINT || undefined; -// getAwsLambdaEndpoint is used to determine the lambda endpoint of the application -export const getAwsDynanmoDbEndpoint = () => process.env.AWS_DYNAMODB_ENDPOINT || undefined; +// getAwsDynamoDbEndpoint is used to determine the dynamodb endpoint of the application +export const getAwsDynamoDbEndpoint = () => process.env.AWS_DYNAMODB_ENDPOINT || undefined; -// getAwsLambdaEndpoint is used to determine the lambda endpoint of the application -export const getProgramsS3BucketName = () => process.env.PROGRAMS_S3_BUCKET_NAME; +// getS3BucketName is used to determine the bucket name of the application +export const getS3BucketName = () => process.env.S3_BUCKET_NAME; -// getAwsLambdaEndpoint is used to determine the lambda endpoint of the application -export const getProgramsCacheTableName = () => process.env.PROGRAMS_CACHE_TABLE_NAME; +// getTableName is used to determine the table name of the application +export const getTableName = () => process.env.TABLE_NAME; diff --git a/examples/serverless-localstack-with-s3-and-dynamodb/src/rest/program.ts b/examples/serverless-localstack-with-s3-and-dynamodb/src/rest/program.ts index e294f23..e347e89 100644 --- a/examples/serverless-localstack-with-s3-and-dynamodb/src/rest/program.ts +++ b/examples/serverless-localstack-with-s3-and-dynamodb/src/rest/program.ts @@ -4,10 +4,10 @@ import validator from '@middy/validator'; import { transpileSchema } from '@middy/validator/transpile'; import httpHeaderNormalizer from '@middy/http-header-normalizer'; import httpJsonBodyParser from '@middy/http-json-body-parser'; -import { getPackageVersion, getProgramsS3BucketName } from '../helpers/env'; +import { getPackageVersion, getS3BucketName } from '../helpers/env'; import { uploadFileToS3 } from '../lib/s3'; -type PostProgramEvent = APIGatewayProxyEvent & { +type PostEvent = APIGatewayProxyEvent & { body: { content: string; filePath: string; @@ -48,7 +48,7 @@ const responseSchema = { // TODO: Get rid of the explicit partial type middy.MiddyfiedHandler // and use the implicit type instead. -const handler: middy.MiddyfiedHandler = middy() +const handler: middy.MiddyfiedHandler = middy() .use(httpHeaderNormalizer()) .use(httpJsonBodyParser()) .use( @@ -60,18 +60,18 @@ const handler: middy.MiddyfiedHandler = middy { const { content, filePath } = event.body; - console.log(`Received program ${filePath} with content: ${content}`); + console.log(`Received ${filePath} with content: ${content}`); - const programsS3BucketName = getProgramsS3BucketName(); - if (!programsS3BucketName) throw new Error('Programs S3 Bucket Name is not defined.'); + const s3BucketName = getS3BucketName(); + if (!s3BucketName) throw new Error(' S3 Bucket Name is not defined.'); const packageVersion = getPackageVersion(); if (!packageVersion) throw new Error('Package Version is not defined.'); - console.log(`Uploading to s3://${programsS3BucketName}/${filePath}...`); + console.log(`Uploading to s3://${s3BucketName}/${filePath}...`); await uploadFileToS3({ - bucketName: programsS3BucketName, + bucketName: s3BucketName, filePath, fileContent: Buffer.from(event.body.content, 'utf8'), }); @@ -79,7 +79,7 @@ const handler: middy.MiddyfiedHandler = middy