-
Notifications
You must be signed in to change notification settings - Fork 0
/
create_aws_resources.sh
executable file
·68 lines (51 loc) · 1.86 KB
/
create_aws_resources.sh
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
#!/usr/bin/env bash
set -eu
# Disable pagination
export AWS_PAGER=""
project="switchbot"
role_name="${project}-lambda-execution-role"
aws_account_id="$(aws sts get-caller-identity --query Account --output text)"
# Create IAM Role for Lambda
aws iam create-role \
--role-name $role_name \
--assume-role-policy-document file://aws/assume-role-policy.json
aws iam put-role-policy \
--role-name $role_name \
--policy-name $project \
--policy-document file://aws/permission-policy.json
# Create CloudWatch Log Group
aws logs create-log-group \
--log-group-name "/aws/lambda/${project}"
aws logs put-retention-policy \
--log-group-name "/aws/lambda/${project}" \
--retention-in-days 7
# Create Lambda Layer
aws lambda publish-layer-version \
--layer-name $project \
--zip-file fileb://layer.zip \
--compatible-runtimes python3.11 \
--compatible-architectures x86_64
layer_version="$(aws lambda list-layer-versions --layer-name $project --query 'LayerVersions[0].Version' --output text)"
# Create Lambda Function
aws lambda create-function \
--function-name $project \
--runtime python3.11 \
--timeout 15 \
--memory-size 256 \
--zip-file fileb://function.zip \
--handler main.lambda_handler \
--role "arn:aws:iam::${aws_account_id}:role/${role_name}" \
--layers "arn:aws:lambda:ap-northeast-1:${aws_account_id}:layer:${project}:${layer_version}"
aws lambda add-permission \
--function-name $project \
--statement-id events \
--action lambda:InvokeFunction \
--principal events.amazonaws.com \
--source-arn "arn:aws:events:ap-northeast-1:${aws_account_id}:rule/${project}"
# Create Event Rule as Lambda Trigger
aws events put-rule \
--name $project \
--schedule-expression "cron(0/5 * * * ? *)"
aws events put-targets \
--rule $project \
--targets "Id=${project},Arn=arn:aws:lambda:ap-northeast-1:${aws_account_id}:function:${project}"