Created
January 23, 2016 15:03
-
-
Save chroju/59db35961f4e346e6074 to your computer and use it in GitHub Desktop.
aws-cliでLambdaのScheduled Eventを作成する ref: http://qiita.com/chroju/items/aa65fddadbc18a2e717a
This file contains hidden or 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
| $ aws iam create-role --role-name "lambdaEc2Execution" --asume-role-policy-document file://lambda_role.json |
This file contains hidden or 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
| $ aws events put-targets --rule "StartAtMorning" --targets Arn=arn:aws:lambda:ap-northeast-1:xxxxxxxxxxxx:function:StartEc2,Id=xxxxx |
This file contains hidden or 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
| $ aws iam create-policy --policy-name "StartStopEc2" --policy-document file://start_stop_ec2_policy.json |
This file contains hidden or 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
| $ aws iam attach-role-policy --role-name "lambdaEc2Execution" --policy-arn "arn:aws:iam::aws:policy/StartStopEc2" |
This file contains hidden or 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
| $ zip startEc2.zip startEc2.js |
This file contains hidden or 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
| $ aws lambda create-function \ | |
| --function-name "startEc2" \ | |
| --zip-file fileb://startEc2.zip \ | |
| --runtime "nodejs" \ | |
| --role "arn:aws:iam::XXXXXXXXXXXX:role/lambdaEc2Execution" \ | |
| --handler "startEc2.handler" |
This file contains hidden or 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
| $ aws lambda invoke --function-name "startEc2" --log-type Tail outfile.txt | |
| $ cat outfile.txt | |
| "Started Instance" |
This file contains hidden or 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
| $ aws lambda add-permission \ | |
| --function-name "StartEc2" \ | |
| --statement-id "" \ | |
| --action 'lambda:InvokeFunction' \ | |
| --principal events.amazonaws.com \ | |
| --source-arn arn:aws:events:ap-northeast-1:xxxxxxxxxxxx:rule/StartAtMorning |
This file contains hidden or 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
| $ aws events put-rule --name "StartAtMorning" --schedule-expression "cron(0 23 * * ? *)" --state ENABLED |
This file contains hidden or 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
| { | |
| "Version": "2012-10-17", | |
| "Statement": [ | |
| { | |
| "Effect": "Allow", | |
| "Action": "sts:AssumeRole", | |
| "Principal": { | |
| "Service": "lambda.amazonaws.com" | |
| }, | |
| "Sid": "" | |
| } | |
| ] | |
| } |
This file contains hidden or 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
| { | |
| "Version": "2012-10-17", | |
| "Statement": [ | |
| { | |
| "Sid": "Stmt0000000000001", | |
| "Effect": "Allow", | |
| "Action": [ | |
| "ec2:StartInstances", | |
| "ec2:StopInstances" | |
| ], | |
| "Resource": [ | |
| "arn:aws:ec2:ap-northeast-1:xxxxxxxxxxxx:instance/xxxxxxxxxx" # 対象インスタンスのARN | |
| ] | |
| } | |
| ] | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment