-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathMakefile
200 lines (162 loc) · 6.35 KB
/
Makefile
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
SECRETS_FILE ?= secrets.mk
ifeq ($(shell test -e $(SECRETS_FILE) && echo -n yes),yes)
include $(SECRETS_FILE)
endif
CUSTOM_FILE ?= custom.mk
ifeq ($(shell test -e $(CUSTOM_FILE) && echo -n yes),yes)
include $(CUSTOM_FILE)
endif
ifndef AWS_PROFILE
AWS_PROFILE = default
endif
LAYER_NAME ?= headless-chromium-layer
LAYER_DESC ?=headless-chromium-layer
S3BUCKET ?= pahud-me-tmp-nrt
LAMBDA_REGION ?= ap-northeast-1
LAMBDA_FUNC_NAME ?= headless-chromium-layer-test-func
LAMBDA_FUNC_DESC ?= headless-chromium-layer-test-func
LAMBDA_ROLE_ARN ?= arn:aws:iam::643758503024:role/LambdaRoleWithS3Upload
build:
@bash build.sh
layer-zip:
( rm -f layer.zip; cd layer; zip -r ../layer.zip * )
layer-upload:
@aws s3 cp layer.zip s3://$(S3BUCKET)/$(LAYER_NAME).zip
layer-publish:
@aws --region $(LAMBDA_REGION) lambda publish-layer-version \
--layer-name $(LAYER_NAME) \
--description $(LAYER_DESC) \
--license-info "MIT" \
--content S3Bucket=$(S3BUCKET),S3Key=$(LAYER_NAME).zip \
--compatible-runtimes provided
func-zip:
chmod +x main.sh
rm -f func-bundle.zip
zip -r func-bundle.zip bootstrap main.sh; ls -alh func-bundle.zip
# zip -r func-bundle.zip bootstrap main.sh .fonts .fontconfig; ls -alh func-bundle.zip
create-func: func-zip
@aws --region $(LAMBDA_REGION) lambda create-function \
--function-name $(LAMBDA_FUNC_NAME) \
--description $(LAMBDA_FUNC_DESC) \
--runtime provided \
--role $(LAMBDA_ROLE_ARN) \
--timeout 30 \
--memory-size 1536 \
--layers $(LAMBDA_LAYERS) \
--handler main \
--zip-file fileb://func-bundle.zip
update-func: func-zip
@aws --region $(LAMBDA_REGION) lambda update-function-code \
--function-name $(LAMBDA_FUNC_NAME) \
--zip-file fileb://func-bundle.zip
func-all: func-zip update-func
layer-all: build layer-upload layer-publish
invoke:
@aws --region $(LAMBDA_REGION) lambda invoke --function-name $(LAMBDA_FUNC_NAME) \
--payload "" lambda.output --log-type Tail | jq -r .LogResult | base64 -d
.PHONY: sam-layer-package
sam-layer-package:
@docker run -ti \
-v $(PWD):/home/samcli/workdir \
-v $(HOME)/.aws:/home/samcli/.aws \
-w /home/samcli/workdir \
-e AWS_DEFAULT_REGION=$(LAMBDA_REGION) \
-e AWS_PROFILE=$(AWS_PROFILE) \
pahud/aws-sam-cli:latest sam package --template-file sam-layer.yaml --s3-bucket $(S3BUCKET) --output-template-file sam-layer-packaged.yaml
@echo "[OK] Now type 'make sam-layer-deploy' to deploy your Lambda layer with SAM"
.PHONY: sam-layer-publish
sam-layer-publish:
@docker run -ti \
-v $(PWD):/home/samcli/workdir \
-v $(HOME)/.aws:/home/samcli/.aws \
-w /home/samcli/workdir \
-e AWS_DEFAULT_REGION=$(LAMBDA_REGION) \
pahud/aws-sam-cli:latest sam publish --region $(LAMBDA_REGION) --template sam-layer-packaged.yaml
.PHONY: sam-layer-deploy
sam-layer-deploy:
@docker run -ti \
-v $(PWD):/home/samcli/workdir \
-v $(HOME)/.aws:/home/samcli/.aws \
-w /home/samcli/workdir \
-e AWS_DEFAULT_REGION=$(LAMBDA_REGION) \
-e AWS_PROFILE=$(AWS_PROFILE) \
pahud/aws-sam-cli:latest sam deploy --template-file ./sam-layer-packaged.yaml --stack-name "$(LAYER_NAME)-stack" \
--parameter-overrides LayerName=$(LAYER_NAME) \
# print the cloudformation stack outputs
aws --region $(LAMBDA_REGION) cloudformation describe-stacks --stack-name "$(LAYER_NAME)-stack" --query 'Stacks[0].Outputs'
@echo "[OK] Layer version deployed."
.PHONY: sam-layer-info
sam-layer-info:
@aws --profile=$(AWS_PROFILE) --region $(LAMBDA_REGION) cloudformation describe-stacks --stack-name "$(LAYER_NAME)-stack" --query 'Stacks[0].Outputs'
.PHONY: sam-layer-add-version-permission
sam-layer-add-version-permission:
@aws --profile=$(AWS_PROFILE) --region $(LAMBDA_REGION) lambda add-layer-version-permission \
--layer-name $(LAYER_NAME) \
--version-number $(LAYER_VER) \
--statement-id public-all \
--action lambda:GetLayerVersion \
--principal '*'
.PHONY: sam-get-layer-version-policy
sam-get-layer-version-policy:
@aws --profile=$(AWS_PROFILE) --region $(LAMBDA_REGION) lambda get-layer-version-policy \
--layer-name $(LAYER_NAME) \
--version-number $(LATEST_LAYER_VER)
.PHONY: sam-layer-add-version-permission-latest
sam-layer-add-version-permission-latest:
@aws --profile=$(AWS_PROFILE) --region $(LAMBDA_REGION) lambda add-layer-version-permission \
--layer-name $(LAYER_NAME) \
--version-number $(LATEST_LAYER_VER) \
--statement-id public-all \
--action lambda:GetLayerVersion \
--principal '*'
.PHONY: sam-layer-destroy
sam-layer-destroy:
# destroy the layer stack
aws --profile=$(AWS_PROFILE) --region $(LAMBDA_REGION) cloudformation delete-stack --stack-name "$(LAYER_NAME)-stack"
@echo "[OK] Layer version destroyed."
add-layer-version-permission:
@aws --region $(LAMBDA_REGION) lambda add-layer-version-permission \
--layer-name $(LAYER_NAME) \
--version-number $(LAYER_VER) \
--statement-id public-all \
--action lambda:GetLayerVersion \
--principal '*'
.PHONY: sam-package
sam-package:
@docker run -ti \
-v $(PWD):/home/samcli/workdir \
-v $(HOME)/.aws:/home/samcli/.aws \
-w /home/samcli/workdir \
-e AWS_DEFAULT_REGION=$(LAMBDA_REGION) \
-e AWS_PROFILE=$(AWS_PROFILE) \
pahud/aws-sam-cli:latest sam package --template-file sam.yaml --s3-bucket $(S3BUCKET) --output-template-file packaged.yaml
.PHONY: sam-deploy
sam-deploy:
@docker run -ti \
-v $(PWD):/home/samcli/workdir \
-v $(HOME)/.aws:/home/samcli/.aws \
-w /home/samcli/workdir \
-e AWS_DEFAULT_REGION=$(LAMBDA_REGION) \
-e AWS_PROFILE=$(AWS_PROFILE) \
pahud/aws-sam-cli:latest sam deploy \
--parameter-overrides \
FunctionName=$(LAMBDA_FUNC_NAME) \
FunctionRole=$(LAMBDA_ROLE_ARN) \
LayerArn=$(LayerArn) \
--template-file ./packaged.yaml --stack-name "$(LAMBDA_FUNC_NAME)-stack" --capabilities CAPABILITY_IAM CAPABILITY_AUTO_EXPAND
# print the cloudformation stack outputs
aws --region $(LAMBDA_REGION) cloudformation describe-stacks --stack-name "$(LAMBDA_FUNC_NAME)-stack" --query 'Stacks[0].Outputs'
.PHONY: sam-destroy
sam-destroy:
# destroy the stack
aws --profile=$(AWS_PROFILE) --region $(LAMBDA_REGION) cloudformation delete-stack --stack-name "$(LAMBDA_FUNC_NAME)-stack"
.PHONY: func-prep
func-prep:
@[ ! -d ./func.d ] && mkdir ./func.d || true
@cp main.sh bootstrap libs.sh ./func.d
all: build layer-upload layer-publish
clean:
rm -rf awscli-bundle* layer layer.zip func-bundle.zip lambda.output .font*
delete-func:
@aws --region $(LAMBDA_REGION) lambda delete-function --function-name $(LAMBDA_FUNC_NAME)
clean-all: clean delete-func