forked from aws-samples/serverless-patterns
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtemplate.yml
executable file
·144 lines (134 loc) · 4.43 KB
/
template.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: Serverless pattern API Gateway to EventBridge (uksb-1tthgi812) (tag:apigw-eventbridge)
Parameters:
Stage:
Type: String
Default: dev
Service:
Type: String
Default: apigw2eb
SubDomainName:
Type: String
# Create a custom domain name at
# https://<REGION>.console.aws.amazon.com/apigateway/main/publish/domain-names
Default: dev-events
DomainName:
Type: String
# Replace example.com with your domain
Default: example.com
# Resources declares the AWS resources that you want to include in the stack
# https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/resources-section-structure.html
Resources:
# Resource creates or updates a partner event bus or custom event bus
# https://docs.aws.amazon.com/ja_jp/AWSCloudFormation/latest/UserGuide/aws-resource-events-eventbus.html
ApplicationEventBus:
Type: AWS::Events::EventBus
Properties:
Name: !Sub ${Service}-${Stage}
# Resource reates an API
# https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-apigatewayv2-api.html
HttpApi:
Type: AWS::ApiGatewayV2::Api
Properties:
Name: !Sub ${Service}-http-api-${Stage}
ProtocolType: HTTP
DisableExecuteApiEndpoint: true
# Resource creates an API mapping
# https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-apigatewayv2-apimapping.html
HttpApiMapping:
DependsOn:
- HttpApiStage
Type: AWS::ApiGatewayV2::ApiMapping
Properties:
ApiId:
Ref: HttpApi
DomainName: !Sub ${SubDomainName}.${DomainName}
Stage: !Sub ${Stage}
ApiMappingKey: !Sub ${Service}
# Resource updates a stage for an API
# https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-apigatewayv2-stage.html
HttpApiStage:
Type: AWS::ApiGatewayV2::Stage
Properties:
ApiId:
Ref: HttpApi
StageName: !Sub ${Stage}
AutoDeploy: true
# Resource creates a route for an API
# https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-apigatewayv2-route.html
HttpApiRoute:
DependsOn:
- HttpApiIntegrationEventBridge
Type: AWS::ApiGatewayV2::Route
Properties:
ApiId:
Ref: HttpApi
RouteKey: POST /{source}/{detailType}
Target:
Fn::Join:
- /
- - integrations
- Ref: HttpApiIntegrationEventBridge
# Resource creates an integration for an API
# https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-apigatewayv2-integration.html
HttpApiIntegrationEventBridge:
DependsOn:
- HttpApiIntegrationEventBridgeRole
Type: AWS::ApiGatewayV2::Integration
Properties:
ApiId:
Ref: HttpApi
IntegrationType: AWS_PROXY
IntegrationSubtype: EventBridge-PutEvents
CredentialsArn:
Fn::GetAtt: [HttpApiIntegrationEventBridgeRole, Arn]
RequestParameters:
# Replace `mycompany` with your needs
Source: com.mycompany.$request.path.source
DetailType: $request.path.detailType
Detail: $request.body
EventBusName:
Fn::GetAtt: [ApplicationEventBus, Arn]
PayloadFormatVersion: "1.0"
TimeoutInMillis: 10000
# Resource creates a new role for your AWS account
# https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-iam-role.html
HttpApiIntegrationEventBridgeRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Principal:
Service:
- apigateway.amazonaws.com
Action:
- sts:AssumeRole
Policies:
- PolicyName: !Sub ${Service}-${Stage}-eventBridge
PolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Action: 'events:*'
Resource:
Fn::GetAtt: [ApplicationEventBus, Arn]
RoleName:
Fn::Join:
- "-"
- - !Sub ${Service}-${Stage}
- Ref: AWS::Region
- eventBridgeRole
Outputs:
ApplicationEventBusName:
Description: Application EventBus Name
Value:
Ref: ApplicationEventBus
ApplicationEventBusArn:
Description: Application EventBus ARN
Value:
Fn::GetAtt:
- ApplicationEventBus
- Arn