-
Notifications
You must be signed in to change notification settings - Fork 0
/
Makefile
116 lines (94 loc) · 4.52 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
include Makefile.mk
NAME=aws-ec2-instance-reaper
AWS_REGION=eu-central-1
S3_BUCKET_PREFIX=binxio-public
S3_BUCKET=$(S3_BUCKET_PREFIX)-$(AWS_REGION)
ALL_REGIONS=$(shell aws --region $(AWS_REGION) \
ec2 describe-regions \
--query 'join(`\n`, Regions[?RegionName != `$(AWS_REGION)`].RegionName)' \
--output text)
help: ## Show this help.
@fgrep -h "##" $(MAKEFILE_LIST) | grep -v fgrep | sed -e 's/\([^:]*\):[^#]*##\(.*\)/printf '"'%-20s - %s\\\\n' '\1' '\2'"'/' |bash
do-push: deploy
do-build: target/$(NAME)-$(VERSION).zip
upload-dist: ## to pypi
rm -f dist/*
python -mbuild
twine upload dist/*
target/$(NAME)-$(VERSION).zip: src/*/*.py setup.cfg pyproject.toml Dockerfile.lambda
mkdir -p target
docker build --build-arg ZIPFILE=$(NAME)-$(VERSION).zip -t $(NAME)-lambda:$(VERSION) -f Dockerfile.lambda . && \
ID=$$(docker create $(NAME)-lambda:$(VERSION) /bin/true) && \
docker export $$ID | (cd target && tar -xvf - $(NAME)-$(VERSION).zip) && \
docker rm -f $$ID && \
chmod ugo+r target/$(NAME)-$(VERSION).zip
clean: ## all intermediate files
rm -rf target dist
find . -name \*.pyc | xargs rm
test: ## run unit tests
tox run
for i in $$PWD/cloudformation/*; do \
aws cloudformation validate-template --template-body file://$$i > /dev/null || exit 1; \
done
fmt: ## sources using black
black src/ tests/
deploy: target/$(NAME)-$(VERSION).zip ## lambda zip to the region $(AWS_REGION)
aws s3 --region $(AWS_REGION) \
cp --acl \
public-read target/$(NAME)-$(VERSION).zip \
s3://$(S3_BUCKET)/lambdas/$(NAME)-$(VERSION).zip
aws s3 --region $(AWS_REGION) \
cp --acl public-read \
s3://$(S3_BUCKET)/lambdas/$(NAME)-$(VERSION).zip \
s3://$(S3_BUCKET)/lambdas/$(NAME)-latest.zip
deploy-all-regions: deploy ## lambda zip to all AWS regions
@for REGION in $(ALL_REGIONS); do \
echo "copying to region $$REGION.." ; \
aws s3 --region $$REGION \
cp --acl public-read \
s3://$(S3_BUCKET_PREFIX)-$(AWS_REGION)/lambdas/$(NAME)-$(VERSION).zip \
s3://$(S3_BUCKET_PREFIX)-$$REGION/lambdas/$(NAME)-$(VERSION).zip; \
aws s3 --region $$REGION \
cp --acl public-read \
s3://$(S3_BUCKET_PREFIX)-$$REGION/lambdas/$(NAME)-$(VERSION).zip \
s3://$(S3_BUCKET_PREFIX)-$$REGION/lambdas/$(NAME)-latest.zip; \
done
deploy-lambda: ## to your AWS account
sed -i -e "s^lambdas/$(NAME).*\.zip^lambdas/$(NAME)-$(VERSION).zip^g" cloudformation/aws-ec2-instance-reaper.yaml
aws cloudformation deploy \
--capabilities CAPABILITY_IAM \
--stack-name $(NAME) \
--template-file ./cloudformation/aws-ec2-instance-reaper.yaml \
--parameter-override \
LambdaS3Bucket=$(S3_BUCKET)
delete-lambda: ## from your AWS account
aws cloudformation delete-stack --stack-name $(NAME)
aws cloudformation wait stack-delete-complete --stack-name $(NAME)
deploy-pipeline: ## to your AWS account
aws cloudformation deploy \
--capabilities CAPABILITY_IAM \
--stack-name $(NAME)-pipeline \
--template-file ./cloudformation/cicd-pipeline.yaml
delete-pipeline: ## from your AWS account
aws cloudformation delete-stack --stack-name $(NAME)-pipeline
aws cloudformation wait stack-delete-complete --stack-name $(NAME)-pipeline
demo: ## to your AWS account
export VPC_ID=$$(aws ec2 --output text --query 'Vpcs[?IsDefault].VpcId' describe-vpcs) ; \
export SUBNET_IDS=$$(aws ec2 --output text --query 'Subnets[*].SubnetId' \
describe-subnets --filters Name=vpc-id,Values=$$VPC_ID | tr '\t' ','); \
echo "deploy $(NAME)-demo in default VPC $$VPC_ID, subnets $$SUBNET_IDS" ; \
([[ -z $$VPC_ID ]] || [[ -z $$SUBNET_IDS ]] ) && \
echo "Either there is no default VPC in your account or there are no subnets in the default VPC" && exit 1 ; \
aws cloudformation deploy --stack-name $(NAME)-demo \
--template-file ./cloudformation/demo-stack.yaml \
--parameter-overrides VPC=$$VPC_ID Subnets=$$SUBNET_IDS
delete-demo: ## from your AWS account
aws cloudformation delete-stack --stack-name $(NAME)-demo
aws cloudformation wait stack-delete-complete --stack-name $(NAME)-demo
make-bucket: ## to store the lambda zip file
aws s3 mb $(S3_BUCKET) || echo "warning bucket already exists"
aws s3api put-public-access-block --bucket $(S3_BUCKET) \
--public-access-block-configuration \
'BlockPublicAcls=false,IgnorePublicAcls=false,BlockPublicPolicy=false,RestrictPublicBuckets=false'
aws s3api put-bucket-ownership-controls --bucket $(S3_BUCKET)\
--ownership-controls 'Rules=[{ObjectOwnership="BucketOwnerPreferred"}]'