Skip to content

Instantly share code, notes, and snippets.

@chroju
Created January 23, 2016 15:03
Show Gist options
  • Select an option

  • Save chroju/59db35961f4e346e6074 to your computer and use it in GitHub Desktop.

Select an option

Save chroju/59db35961f4e346e6074 to your computer and use it in GitHub Desktop.
aws-cliでLambdaのScheduled Eventを作成する ref: http://qiita.com/chroju/items/aa65fddadbc18a2e717a
$ aws iam create-role --role-name "lambdaEc2Execution" --asume-role-policy-document file://lambda_role.json
$ aws events put-targets --rule "StartAtMorning" --targets Arn=arn:aws:lambda:ap-northeast-1:xxxxxxxxxxxx:function:StartEc2,Id=xxxxx
$ aws iam create-policy --policy-name "StartStopEc2" --policy-document file://start_stop_ec2_policy.json
$ aws iam attach-role-policy --role-name "lambdaEc2Execution" --policy-arn "arn:aws:iam::aws:policy/StartStopEc2"
$ zip startEc2.zip startEc2.js
$ aws lambda create-function \
--function-name "startEc2" \
--zip-file fileb://startEc2.zip \
--runtime "nodejs" \
--role "arn:aws:iam::XXXXXXXXXXXX:role/lambdaEc2Execution" \
--handler "startEc2.handler"
$ aws lambda invoke --function-name "startEc2" --log-type Tail outfile.txt
$ cat outfile.txt
"Started Instance"
$ 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
$ aws events put-rule --name "StartAtMorning" --schedule-expression "cron(0 23 * * ? *)" --state ENABLED
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": "sts:AssumeRole",
"Principal": {
"Service": "lambda.amazonaws.com"
},
"Sid": ""
}
]
}
{
"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