This repository has been archived by the owner on Sep 25, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
SpotFleet CloudFormation #69
Merged
+420
−0
Merged
Changes from 7 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
9284a4d
[CloudFormation] Add ApiGateway and StepFunctions for webhook endpoint
f009c38
[CloudFormation] Add RunEcsTaskFunction
3ad17c6
[CloudFormation] Add deploy script
d640683
set sample SpotFleetRequest for Ecs
selmertsx 28fffd0
[CloudFormation] set correct subnet to spot fleet instance
selmertsx e4cb0dc
[CloudFormation] set correct Instance Type to SpotFleet Request
selmertsx 0c162c4
[CloudFormation] set another container instance for spot fleet request
selmertsx c0084f1
[CloudFormation] set IAM Role for SpotFleet
selmertsx f32f655
[CloudFormation] set ManagedPolicyArns to FleetIAMRole
selmertsx 85486c9
[CloudFormation] parameterize SpotPrice and TargetCapacity in SpotFle…
selmertsx cc92b41
[CloudFormation] add NetworkInterface to EC2 ContainerInstances
selmertsx 56c3e83
[CloudFormation] fix service settings to IAM Role for SpotFleet Request
selmertsx 9438bba
[CloudFormation] set ManagedPolicyArns for FleetIAMRole
selmertsx 0225f65
[CloudFormation] remove Network Interfaces properties for spot fleet …
selmertsx 7656843
[CloudFormation] Parameterize instance type for SpotFleetRequest
selmertsx File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
## How to Deploy | ||
|
||
```bash | ||
# BUCKET_NAME is used for `aws cloudformation package` | ||
# STACK_NAME is used for `aws cloudformation deploy` | ||
$ BUCKET_NAME=bucket-name STACK_NAME=stack-name bin/deploy | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
#!/bin/bash | ||
|
||
if [ -z "$BUCKET_NAME" ]; then | ||
echo 'BUCKET_NAME is required' | ||
exit 1 | ||
fi | ||
|
||
if [ -z "$STACK_NAME" ]; then | ||
echo 'STACK_NAME is required' | ||
exit 1 | ||
fi | ||
|
||
TEMPLATE_FILE=cloudformation.yml | ||
|
||
# Move project root | ||
cd "$(dirname "$(perl -e 'use Cwd "abs_path";print abs_path(shift)' "$0")")/.." || exit 1 | ||
|
||
sh -c 'cd ./functions/RunEcsTask && npm install && npm run build && cp -rf ./node_modules ./built/' | ||
|
||
mkdir -p ./built | ||
|
||
aws cloudformation package \ | ||
--template-file "$TEMPLATE_FILE" \ | ||
--output-template-file ./built/cloudformation.yml \ | ||
--s3-bucket "$BUCKET_NAME" | ||
|
||
aws cloudformation deploy \ | ||
--capabilities CAPABILITY_NAMED_IAM \ | ||
--template-file built/cloudformation.yml \ | ||
--stack-name "$STACK_NAME" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
AWSTemplateFormatVersion: "2010-09-09" | ||
Transform: AWS::Serverless-2016-10-31 | ||
Description: revieee development settings | ||
Resources: | ||
|
||
|
@@ -163,6 +164,28 @@ Resources: | |
Properties: | ||
ClusterName: !Join [ "-", [ !Ref "AWS::StackName", RevieeeCluster ] ] | ||
|
||
# SpotFleet | ||
EcsSpotFleetRequest: | ||
Type: AWS::EC2::SpotFleet | ||
Properties: | ||
SpotFleetRequestConfigData: | ||
IamFleetRole: !GetAtt [IAMFleetRole, Arn] | ||
SpotPrice: '0.139' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. このあたりはパラメータ設定出来るようにする! |
||
TargetCapacity: '2' | ||
LaunchSpecifications: | ||
- EbsOptimized: 'false' | ||
InstanceType: 'm4.large' | ||
ImageId: 'ami-3a000e5d' | ||
SubnetId: | ||
Ref: ContainerInstanceSubnet1a | ||
WeightedCapacity: '4' | ||
- EbsOptimizes: 'false' | ||
InstanceType: 'm3.large' | ||
ImageId: 'ami-3a000e5d' | ||
SubnetId: | ||
Ref: ContainerInstanceSubnet1c | ||
WeightedCapacity: '1' | ||
|
||
EndpointInstanceIamRole: | ||
Type: AWS::IAM::Role | ||
Properties: | ||
|
@@ -199,6 +222,159 @@ Resources: | |
Value: RevieeeEndpointInstance | ||
# UserData: [TODO] run itamae | ||
|
||
# ------ API Gateway ------ | ||
|
||
ApiRole: | ||
Type: AWS::IAM::Role | ||
Properties: | ||
AssumeRolePolicyDocument: | ||
Version: 2012-10-17 | ||
Statement: | ||
- | ||
Effect: Allow | ||
Principal: | ||
Service: | ||
- apigateway.amazonaws.com | ||
Action: | ||
- sts:AssumeRole | ||
Policies: | ||
- PolicyDocument: | ||
Version: 2012-10-17 | ||
Statement: | ||
- Effect: Allow | ||
Action: "states:*" | ||
Resource: "*" | ||
PolicyName: !Join [ "-", [ !Ref "AWS::StackName", AWSStepFunctionsFullAccess ] ] | ||
RoleName: !Join [ "-", [ !Ref "AWS::StackName", RevieeeApiRole ] ] | ||
|
||
RevieeeApi: | ||
Type: AWS::ApiGateway::RestApi | ||
Properties: | ||
Name: !Join [ "-", [ !Ref "AWS::StackName", RevieeeApi ] ] | ||
RevieeeApiResource: | ||
Type: AWS::ApiGateway::Resource | ||
Properties: | ||
RestApiId: !Ref RevieeeApi | ||
ParentId: !GetAtt RevieeeApi.RootResourceId | ||
PathPart: "stage" | ||
RevieeeApiCreateMethod: | ||
Type: AWS::ApiGateway::Method | ||
Properties: | ||
RestApiId: !Ref RevieeeApi | ||
ResourceId: !Ref RevieeeApiResource | ||
AuthorizationType: NONE | ||
HttpMethod: POST | ||
Integration: | ||
IntegrationHttpMethod: POST | ||
IntegrationResponses: | ||
- StatusCode: 200 | ||
PassthroughBehavior: WHEN_NO_TEMPLATES | ||
RequestTemplates: | ||
application/json: !Sub | ||
- |- | ||
{ | ||
"input": "{}", | ||
"name": "Hello", | ||
"stateMachineArn": "${stateMachineArn}" | ||
} | ||
- { stateMachineArn: !Ref StateMachineCreate } | ||
Type: AWS | ||
Uri: !Join [ "", [ "arn:aws:apigateway:", !Ref "AWS::Region", ":states:action/StartExecution" ] ] | ||
Credentials: !GetAtt ApiRole.Arn | ||
MethodResponses: | ||
- StatusCode: 200 | ||
ApiDeployment: | ||
Type: AWS::ApiGateway::Deployment | ||
DependsOn: RevieeeApiCreateMethod | ||
Properties: | ||
RestApiId: !Ref RevieeeApi | ||
StageName: pub | ||
|
||
# ------ /API Gateway ------ | ||
|
||
# ------ StepFunctions ------ | ||
|
||
StateMachineRole: | ||
Type: AWS::IAM::Role | ||
Properties: | ||
AssumeRolePolicyDocument: | ||
Version: 2012-10-17 | ||
Statement: | ||
- | ||
Effect: Allow | ||
Principal: | ||
Service: | ||
- states.ap-northeast-1.amazonaws.com | ||
Action: | ||
- sts:AssumeRole | ||
Policies: | ||
- PolicyDocument: | ||
Version: 2012-10-17 | ||
Statement: | ||
- Effect: Allow | ||
Action: "lambda:InvokeFunction" | ||
Resource: "*" | ||
PolicyName: !Join [ "-", [ !Ref "AWS::StackName", StatesExecutionPolicy ] ] | ||
RoleName: !Join [ "-", [ !Ref "AWS::StackName", RevieeeStateMachineRole ] ] | ||
|
||
StateMachineCreate: | ||
Type: AWS::StepFunctions::StateMachine | ||
Properties: | ||
DefinitionString: !Sub | ||
- |- | ||
{ | ||
"Comment": "Run ECS Task", | ||
"StartAt": "RunEcsTask", | ||
"States": { | ||
"RunEcsTask": { | ||
"Type": "Task", | ||
"Resource": "${taskArn}", | ||
"End": true | ||
} | ||
} | ||
} | ||
- { "taskArn": !Ref RunEcsTaskFunction } | ||
RoleArn: !GetAtt StateMachineRole.Arn | ||
|
||
# ------ /StepFunctions ------ | ||
|
||
# ------ Lambda ------ | ||
|
||
RunEcsTaskFunctionRole: | ||
Type: AWS::IAM::Role | ||
Properties: | ||
AssumeRolePolicyDocument: | ||
Version: 2012-10-17 | ||
Statement: | ||
- | ||
Effect: Allow | ||
Principal: | ||
Service: | ||
- lambda.amazonaws.com | ||
Action: | ||
- sts:AssumeRole | ||
Policies: | ||
- PolicyDocument: | ||
Version: 2012-10-17 | ||
Statement: | ||
- Effect: Allow | ||
Action: "ecs:RunTask" | ||
Resource: "*" | ||
PolicyName: !Join [ "-", [ !Ref "AWS::StackName", RunEcsTaskFunctionPolicy ] ] | ||
ManagedPolicyArns: | ||
- 'arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole' | ||
RoleName: !Join [ "-", [ !Ref "AWS::StackName", RevieeeRunEcsTaskFunctionRole ] ] | ||
|
||
RunEcsTaskFunction: | ||
Type: AWS::Serverless::Function | ||
Properties: | ||
Handler: index.handler | ||
Runtime: nodejs6.10 | ||
CodeUri: ./functions/RunEcsTask/built/ | ||
Role: !GetAtt RunEcsTaskFunctionRole.Arn | ||
|
||
# ------ /Lambda ------ | ||
|
||
# Parameter | ||
Parameters: | ||
SSHPort: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
{ | ||
"name": "run-ecs-task", | ||
"version": "0.0.1", | ||
"description": "Lambda function that to run ECS Task", | ||
"main": "index.js", | ||
"scripts": { | ||
"build": "tsc -p ./tsconfig.json", | ||
"clean": "rm -rf ./built" | ||
}, | ||
"author": "", | ||
"license": "MIT", | ||
"repository": { | ||
"type": "git", | ||
"url": "git://github.com/speee/webapp-revieee" | ||
}, | ||
"devDependencies": { | ||
"@types/aws-lambda": "0.0.12", | ||
"@types/node": "^7.0.31", | ||
"aws-sdk": "^2.71.0", | ||
"tslint": "^5.4.3", | ||
"typescript": "^2.3.4" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { Callback, Context } from "aws-lambda"; | ||
import * as AWS from "aws-sdk"; | ||
|
||
const ecs = new AWS.ECS(); | ||
|
||
export function handler(event: any, context: Context, callback: Callback) { | ||
const envBranch: AWS.ECS.KeyValuePair = { | ||
name: "BRANCH", | ||
value: "master", | ||
}; | ||
const containerOverride: AWS.ECS.ContainerOverride = { | ||
name: "main", | ||
environment: [envBranch], | ||
}; | ||
const taskOverride: AWS.ECS.TaskOverride = { | ||
containerOverrides: [containerOverride], | ||
}; | ||
const params: AWS.ECS.RunTaskRequest = { | ||
cluster: "revieee", | ||
taskDefinition: "arn:aws:ecs:ap-northeast-1:951787653356:task-definition/im-ieul-core:3", | ||
overrides: taskOverride, | ||
}; | ||
|
||
(async () => { | ||
return await ecs.runTask(params).promise(); | ||
})().then((result: AWS.ECS.RunTaskResponse) => { | ||
console.log(result.tasks); | ||
callback(null, result.tasks[0].taskArn); | ||
}).catch((err: AWS.AWSError) => { | ||
callback(err); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
{ | ||
"compilerOptions": { | ||
"module": "commonjs", | ||
"target": "es2015", | ||
"noImplicitAny": true, | ||
"noImplicitReturns": true, | ||
"noFallthroughCasesInSwitch": true, | ||
"outDir": "./built", | ||
"allowJs": true | ||
}, | ||
"include": [ | ||
"./src/**/*" | ||
] | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
=> IAMFleetRole.Arn