Skip to content

Commit

Permalink
Updated example
Browse files Browse the repository at this point in the history
  • Loading branch information
ulises-jeremias committed Nov 5, 2023
1 parent 2528370 commit 09e2397
Show file tree
Hide file tree
Showing 9 changed files with 39 additions and 37 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Deployment Configuration
SERVICE_NAME=serverless-localstack-example

PROGRAMS_S3_BUCKET_NAME=serverless-localstack-bucket
S3_BUCKET_NAME=serverless-localstack-bucket
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Deployment Configuration
SERVICE_NAME=serverless-localstack-example

PROGRAMS_S3_BUCKET_NAME=serverless-localstack-bucket
S3_BUCKET_NAME=serverless-localstack-bucket
6 changes: 4 additions & 2 deletions examples/serverless-localstack-with-s3-and-dynamodb/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}'
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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)}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<PostProgramEvent, APIGatewayProxyResult>()
const handler: middy.MiddyfiedHandler = middy<PostEvent, APIGatewayProxyResult>()
.use(httpHeaderNormalizer())
.use(httpJsonBodyParser())
.use(
Expand All @@ -60,26 +60,26 @@ const handler: middy.MiddyfiedHandler = middy<PostProgramEvent, APIGatewayProxyR
.handler(async (event) => {
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'),
});

return {
statusCode: 200,
body: JSON.stringify({
message: 'Program uploaded successfully to S3.',
message: ' uploaded successfully to S3.',
}),
};
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
program:
handler: src/rest/program.handler
example:
handler: src/rest/example.handler
events:
- http:
method: POST
path: /program
path: /

0 comments on commit 09e2397

Please sign in to comment.