Skip to content

Commit 21b8089

Browse files
authored
Merge pull request #795 from serverless/compose-example
feat(compose): add SAM/CFN + Compose example
2 parents 5aaf492 + 95c2923 commit 21b8089

File tree

7 files changed

+124
-0
lines changed

7 files changed

+124
-0
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
AWSTemplateFormatVersion: "2010-09-09"
2+
3+
Resources:
4+
SharedTable:
5+
Type: "AWS::DynamoDB::Table"
6+
Properties:
7+
TableName: ${param:tableNamePrefix}-${param:stageLabel}
8+
AttributeDefinitions:
9+
- AttributeName: "Id"
10+
AttributeType: "S"
11+
KeySchema:
12+
- AttributeName: "Id"
13+
KeyType: "HASH"
14+
ProvisionedThroughput:
15+
ReadCapacityUnits: 5
16+
WriteCapacityUnits: 5
17+
18+
Outputs:
19+
TableName:
20+
Value: !Ref SharedTable
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
exports.handler = async (event) => {
2+
return {
3+
tableName: process.env.TABLE_NAME,
4+
stageLabel: process.env.STAGE_LABEL,
5+
service: "sam-compose-service",
6+
message:
7+
"This is the staged table name that was constructed using the compose outputs and params",
8+
};
9+
};
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
version = 0.1
2+
3+
[default]
4+
[default.global.parameters]
5+
stack_name = "sam-compose-service-example"
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
AWSTemplateFormatVersion: "2010-09-09"
2+
Transform: AWS::Serverless-2016-10-31
3+
Description: >
4+
sam-app
5+
6+
Sample SAM Template for sam-app
7+
8+
Globals:
9+
Function:
10+
Timeout: 3
11+
MemorySize: 128
12+
13+
Resources:
14+
HelloWorldFunction:
15+
Type: AWS::Serverless::Function
16+
Properties:
17+
Handler: handler.handler
18+
Runtime: nodejs20.x
19+
Environment:
20+
Variables:
21+
TABLE_NAME: ${param:tableName}
22+
STAGE_LABEL: ${param:stageLabel}
23+
Architectures:
24+
- x86_64
25+
Events:
26+
Api:
27+
Type: HttpApi
28+
Properties:
29+
Path: /
30+
Method: GET
31+
32+
Outputs:
33+
Endpoint:
34+
Value: !Sub "https://${ServerlessHttpApi}.execute-api.${AWS::Region}.amazonaws.com"

compose/serverless-compose.yml

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# This example shows how to use Serverless Compose
2+
# to compose 3 services together:
3+
# - A CloudFormation service
4+
# - A traditional Serverless Framework service
5+
# - A SAM service
6+
# It specifically shows how to pass parameters between services
7+
# and how to reference outputs from one service in another service.
8+
9+
stages:
10+
default:
11+
params:
12+
stageLabel: "testing"
13+
prod:
14+
params:
15+
stageLabel: "production"
16+
17+
services:
18+
cloudformation-compose-service:
19+
path: cloudformation-compose-service
20+
params:
21+
tableNamePrefix: compose-shared-table
22+
23+
traditional-compose-service:
24+
path: traditional-compose-service
25+
params:
26+
tableName: ${cloudformation-compose-service.TableName}
27+
28+
sam-compose-service:
29+
path: sam-compose-service
30+
params:
31+
tableName: ${cloudformation-compose-service.TableName}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
exports.handler = async (event) => {
2+
return {
3+
tableName: process.env.TABLE_NAME,
4+
stageLabel: process.env.STAGE_LABEL,
5+
service: "traditional-compose-service",
6+
message:
7+
"This is the staged table name that was constructed using the compose outputs and params",
8+
};
9+
};
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
service: traditional-compose-service
2+
3+
provider:
4+
name: aws
5+
runtime: nodejs20.x
6+
7+
functions:
8+
hello:
9+
handler: handler.handler
10+
events:
11+
- httpApi:
12+
path: /
13+
method: get
14+
environment:
15+
TABLE_NAME: ${param:tableName}
16+
STAGE_LABEL: ${param:stageLabel}

0 commit comments

Comments
 (0)