Skip to content

Commit 1937729

Browse files
committed
Initial commit
0 parents  commit 1937729

File tree

3 files changed

+361
-0
lines changed

3 files changed

+361
-0
lines changed

README.md

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# Serverless Application
2+
This is serverless application using AWS SAM.
3+
4+
# Installation
5+
## aws-cli
6+
```
7+
$ sudo pip install awscli
8+
```
9+
10+
# Settings
11+
## credential of IAM user
12+
Add IAM user's credential to `~/.aws/credentials`
13+
14+
```
15+
[default]
16+
aws_access_key_id = #{IAM user access key}
17+
aws_secret_access_key = #{IAM user secret token}
18+
```
19+
20+
If you use multiple credentials, use profile.
21+
https://docs.aws.amazon.com/cli/latest/userguide/cli-multiple-profiles.html
22+
23+
# Package
24+
```
25+
aws cloudformation package \
26+
--template-file template.yaml \
27+
--s3-bucket sample-sam-resource \
28+
--output-template-file packaged-template.yaml
29+
```
30+
31+
# Deploy
32+
```
33+
aws cloudformation deploy \
34+
--template-file packaged-template.yaml \
35+
--stack-name sam-sample-stack \
36+
--capabilities CAPABILITY_IAM
37+
```

setup.cfg

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[pycodestyle]
2+
max-line-length = 128

template.yaml

+322
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,322 @@
1+
AWSTemplateFormatVersion: '2010-09-09'
2+
Transform: AWS::Serverless-2016-10-31
3+
Description: Create Lambda function by using AWS SAM.
4+
5+
Globals:
6+
Function:
7+
Runtime: python3.6
8+
Timeout: 15
9+
MemorySize: 256
10+
Environment:
11+
Variables:
12+
TABLE_NAME: !Ref SamSampleDynamoTable
13+
14+
Resources:
15+
SNSRole:
16+
Type: "AWS::IAM::Role"
17+
Properties:
18+
AssumeRolePolicyDocument:
19+
Version: "2012-10-17"
20+
Statement:
21+
- Effect: "Allow"
22+
Principal:
23+
Service:
24+
- "cognito-idp.amazonaws.com"
25+
Action:
26+
- "sts:AssumeRole"
27+
Policies:
28+
- PolicyName: "CognitoSNSPolicy"
29+
PolicyDocument:
30+
Version: "2012-10-17"
31+
Statement:
32+
- Effect: "Allow"
33+
Action: "sns:publish"
34+
Resource: "*"
35+
UserPool:
36+
Type: AWS::Cognito::UserPool
37+
Properties:
38+
AdminCreateUserConfig:
39+
AllowAdminCreateUserOnly: false
40+
UnusedAccountValidityDays: 7
41+
AliasAttributes:
42+
- email
43+
- phone_number
44+
AutoVerifiedAttributes:
45+
- phone_number
46+
- email
47+
EmailVerificationMessage: "Your verification code is {####}."
48+
EmailVerificationSubject: "Your verification code"
49+
MfaConfiguration: "OPTIONAL"
50+
Policies:
51+
PasswordPolicy:
52+
MinimumLength: 8
53+
RequireLowercase: true
54+
RequireNumbers: true
55+
RequireSymbols: true
56+
RequireUppercase: false
57+
UserPoolName:
58+
Ref: AWS::StackName
59+
Schema:
60+
- AttributeDataType: "String"
61+
DeveloperOnlyAttribute: false
62+
Mutable: true
63+
Name: "email"
64+
StringAttributeConstraints:
65+
MaxLength: "2048"
66+
MinLength: "0"
67+
Required: true
68+
- AttributeDataType: "String"
69+
DeveloperOnlyAttribute: false
70+
Mutable: true
71+
Name: "phone_number"
72+
StringAttributeConstraints:
73+
MaxLength: "2048"
74+
MinLength: "0"
75+
Required: true
76+
SmsConfiguration:
77+
ExternalId: !Join
78+
- ''
79+
- - 'external-'
80+
- !Ref "AWS::StackName"
81+
SnsCallerArn: !GetAtt SNSRole.Arn
82+
SmsAuthenticationMessage: "Your authentication code is {####}."
83+
SmsVerificationMessage: "Your verification code is {####}."
84+
UserPoolClient:
85+
Type: AWS::Cognito::UserPoolClient
86+
Properties:
87+
ClientName: JavaScriptClient
88+
GenerateSecret: false
89+
UserPoolId: !Ref UserPool
90+
ReadAttributes:
91+
- email
92+
- email_verified
93+
- phone_number
94+
- phone_number_verified
95+
WriteAttributes:
96+
- email
97+
- phone_number
98+
IdentityPool:
99+
Type: AWS::Cognito::IdentityPool
100+
Properties:
101+
AllowUnauthenticatedIdentities: true
102+
IdentityPoolName: !Ref "AWS::StackName"
103+
CognitoIdentityProviders:
104+
- ClientId: !Ref UserPoolClient
105+
ProviderName:
106+
Fn::Join:
107+
- ""
108+
- - cognito-idp.
109+
- Ref: AWS::Region
110+
- .amazonaws.com/
111+
- Ref: UserPool
112+
AllowUnauthenticatedIdentities: false
113+
UnauthenticatedPolicy:
114+
Type: AWS::IAM::ManagedPolicy
115+
Properties:
116+
PolicyDocument:
117+
Version: "2012-10-17"
118+
Statement:
119+
- Effect: Allow
120+
Action:
121+
- mobileanalytics:PutEvents
122+
- cognito-sync:*
123+
Resource:
124+
- "*"
125+
UnauthenticatedRole:
126+
Type: AWS::IAM::Role
127+
Properties:
128+
AssumeRolePolicyDocument:
129+
Version: "2012-10-17"
130+
Statement:
131+
- Effect: Allow
132+
Action: "sts:AssumeRoleWithWebIdentity"
133+
Principal:
134+
Federated: cognito-identity.amazonaws.com
135+
Condition:
136+
StringEquals:
137+
"cognito-identity.amazonaws.com:aud": !Ref IdentityPool
138+
ForAnyValue:StringLike:
139+
"cognito-identity.amazonaws.com:amr": unauthenticated
140+
ManagedPolicyArns:
141+
- Ref: UnauthenticatedPolicy
142+
AuthenticatedPolicy:
143+
Type: AWS::IAM::ManagedPolicy
144+
Properties:
145+
PolicyDocument:
146+
Version: "2012-10-17"
147+
Statement:
148+
- Effect: Allow
149+
Action:
150+
- mobileanalytics:PutEvents
151+
- cognito-sync:*
152+
- cognito-identity:*
153+
Resource:
154+
- "*"
155+
AuthenticatedRole:
156+
Type: AWS::IAM::Role
157+
Properties:
158+
AssumeRolePolicyDocument:
159+
Version: "2012-10-17"
160+
Statement:
161+
- Effect: Allow
162+
Action: "sts:AssumeRoleWithWebIdentity"
163+
Principal:
164+
Federated: cognito-identity.amazonaws.com
165+
Condition:
166+
StringEquals:
167+
"cognito-identity.amazonaws.com:aud": !Ref IdentityPool
168+
ForAnyValue:StringLike:
169+
"cognito-identity.amazonaws.com:amr": authenticated
170+
ManagedPolicyArns:
171+
- Ref: AuthenticatedPolicy
172+
RoleAttachment:
173+
Type: AWS::Cognito::IdentityPoolRoleAttachment
174+
Properties:
175+
IdentityPoolId: !Ref IdentityPool
176+
Roles:
177+
unauthenticated:
178+
Fn::GetAtt:
179+
- UnauthenticatedRole
180+
- Arn
181+
authenticated:
182+
Fn::GetAtt:
183+
- AuthenticatedRole
184+
- Arn
185+
RestApi:
186+
Type: AWS::Serverless::Api
187+
Properties:
188+
StageName: dev
189+
DefinitionBody:
190+
swagger: "2.0"
191+
info:
192+
title: dev-api
193+
version: 1.0.0
194+
basePath: /
195+
schemes:
196+
- https
197+
paths:
198+
/users:
199+
get:
200+
summary: user index
201+
description: |
202+
Get lists of all users.
203+
consumes:
204+
- application/json
205+
produces:
206+
- application/json
207+
responses:
208+
"200":
209+
description: "responce when request succeed"
210+
schema:
211+
type: "array"
212+
items:
213+
type: "object"
214+
properties:
215+
name:
216+
type: "string"
217+
example: "foo"
218+
"404":
219+
description: "responce when request is failed"
220+
schema:
221+
type: "object"
222+
properties:
223+
error:
224+
type: "string"
225+
example: "404 Not Found"
226+
message:
227+
type: "string"
228+
example: "Can not find resources"
229+
security:
230+
- cognitoUserPool: []
231+
x-amazon-apigateway-integration:
232+
responses:
233+
default:
234+
statusCode: "200"
235+
uri: !Sub arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${UserIndex.Arn}/invocations
236+
passthroughBehavior: when_no_templates
237+
httpMethod: POST
238+
type: aws_proxy
239+
post:
240+
summary: user create
241+
description: |
242+
Create user
243+
consumes:
244+
- application/json
245+
produces:
246+
- application/json
247+
parameters:
248+
- name: name
249+
in: body
250+
description: User name.
251+
required: true
252+
schema:
253+
type: object
254+
properties:
255+
name:
256+
type: "string"
257+
example: "foo"
258+
responses:
259+
"200":
260+
description: "responce when request succeed"
261+
schema:
262+
type: "object"
263+
properties:
264+
name:
265+
type: "string"
266+
example: "foo"
267+
"400":
268+
description: "responce when request is failed"
269+
schema:
270+
type: "object"
271+
properties:
272+
error:
273+
type: "string"
274+
example: "400 Bad Request"
275+
message:
276+
type: "string"
277+
example: "Invalid parameter"
278+
"404":
279+
description: "responce when request is failed"
280+
schema:
281+
type: "object"
282+
properties:
283+
error:
284+
type: "string"
285+
example: "404 Not Found"
286+
message:
287+
type: "string"
288+
example: "Can not find resources"
289+
security:
290+
- cognitoUserPool: []
291+
x-amazon-apigateway-integration:
292+
responses:
293+
default:
294+
statusCode: "200"
295+
uri: !Sub arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${UserCreate.Arn}/invocations
296+
passthroughBehavior: when_no_templates
297+
httpMethod: POST
298+
type: aws_proxy
299+
securityDefinitions:
300+
cognitoUserPool:
301+
type: apiKey
302+
name: Authorization
303+
in: header
304+
x-amazon-apigateway-authtype: cognito_user_pools
305+
x-amazon-apigateway-authorizer:
306+
type: cognito_user_pools
307+
providerARNs:
308+
- !GetAtt UserPool.Arn
309+
LambdaRole:
310+
Type: "AWS::IAM::Role"
311+
Properties:
312+
AssumeRolePolicyDocument:
313+
Version: "2012-10-17"
314+
Statement:
315+
- Effect: "Allow"
316+
Principal:
317+
Service:
318+
- "lambda.amazonaws.com"
319+
Action:
320+
- "sts:AssumeRole"
321+
ManagedPolicyArns:
322+
- arn:aws:iam::aws:policy/AmazonDynamoDBFullAccess

0 commit comments

Comments
 (0)