Skip to content

Commit 0da56cb

Browse files
committed
finalizing the first version
1 parent 58f198d commit 0da56cb

File tree

7 files changed

+118
-90
lines changed

7 files changed

+118
-90
lines changed

Makefile

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,23 @@
11
DEPLOYMENT_BUCKET_NAME := desole-packaging
2-
DEPLOYMENT_KEY := $(shell echo frontend-deploy-$$RANDOM.zip)
3-
STACK_NAME := frontend-deploy-layer
2+
DEPLOYMENT_KEY := $(shell echo s3-deployment-$$RANDOM.zip)
3+
STACK_NAME := s3-deployment
44

55
clean:
66
rm -rf build
77

8-
build/python: src/deployer.py
8+
build/python:
99
mkdir -p build/python
10-
cp -R src/ build/python
1110

12-
build/layer.zip: build/python
13-
cd build/ && zip -r layer.zip python
14-
15-
build/function.zip: lambda
16-
cd lambda/ && zip -r ../build/function.zip *
11+
build/python/deployer.py: src/deployer.py build/python
12+
cp $< $@
1713

18-
# cloudformation has no support for packaging layers yet, so need to do this manually
14+
build/python/requests: build/python
15+
pip install requests --target build/python
1916

20-
build/output.yml: build/layer.zip cloudformation/template.yml build/function.zip
21-
aws s3 cp build/layer.zip s3://$(DEPLOYMENT_BUCKET_NAME)/$(DEPLOYMENT_KEY)/layer.zip
22-
aws s3 cp build/function.zip s3://$(DEPLOYMENT_BUCKET_NAME)/$(DEPLOYMENT_KEY)/function.zip
23-
sed "s:DEPLOYMENT_BUCKET_NAME:$(DEPLOYMENT_BUCKET_NAME):;s:DEPLOYMENT_KEY:$(DEPLOYMENT_KEY):" cloudformation/template.yml > build/output.yml
17+
build/layer.zip: build/python/deployer.py build/python/requests
18+
cd build/ && zip -r layer.zip python
2419

25-
deploy: build/output.yml
26-
aws cloudformation deploy --template-file build/output.yml --stack-name $(STACK_NAME) --capabilities CAPABILITY_IAM
20+
deploy: cloudformation/template.yml build/layer.zip
21+
aws s3 cp build/layer.zip s3://$(DEPLOYMENT_BUCKET_NAME)/$(DEPLOYMENT_KEY)
22+
aws cloudformation deploy --template-file cloudformation/template.yml --stack-name $(STACK_NAME) --capabilities CAPABILITY_IAM --parameter-overrides DeploymentBucketName=$(DEPLOYMENT_BUCKET_NAME) DeploymentKey=$(DEPLOYMENT_KEY)
2723
aws cloudformation describe-stacks --stack-name $(STACK_NAME) --query Stacks[].Outputs[].OutputValue --output text

cloudformation/template.yml

Lines changed: 14 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -1,86 +1,35 @@
1-
Description: Frontend Deploy Layer
1+
AWSTemplateFormatVersion: 2010-09-09
2+
Description: S3 Deployment Layer
23
Parameters:
34
AllowedPrincipal:
45
Type: String
56
Description: Account principal allowed to deploy this layer
67
Default: '*'
8+
DeploymentBucketName:
9+
Type: String
10+
DeploymentKey:
11+
Type: String
712

813
Resources:
9-
FrontendLayer:
14+
S3DeploymentLayer:
1015
Type: AWS::Lambda::LayerVersion
1116
Properties:
1217
CompatibleRuntimes:
1318
- python3.6
14-
Description: Frontend Deploy for AWS Lambda
15-
LayerName: frontend-deploy
19+
Description: S3 Deployment Layer
20+
LayerName: s3-deployment
1621
LicenseInfo: MIT
1722
Content:
18-
S3Bucket: DEPLOYMENT_BUCKET_NAME
19-
S3Key: DEPLOYMENT_KEY/layer.zip
23+
S3Bucket: !Ref DeploymentBucketName
24+
S3Key: !Ref DeploymentKey
2025

2126
DeploymentPermission:
2227
Type: AWS::Lambda::LayerVersionPermission
2328
Properties:
2429
Action: lambda:GetLayerVersion
25-
LayerVersionArn: !Ref FrontendLayer
30+
LayerVersionArn: !Ref S3DeploymentLayer
2631
Principal: !Ref AllowedPrincipal
2732

28-
TargetBucket:
29-
Type: AWS::S3::Bucket
30-
31-
LambdaRole:
32-
Type: 'AWS::IAM::Role'
33-
Properties:
34-
AssumeRolePolicyDocument:
35-
Version: '2012-10-17'
36-
Statement:
37-
- Effect: Allow
38-
Action: 'sts:AssumeRole'
39-
Principal:
40-
Service: lambda.amazonaws.com
41-
Policies:
42-
- PolicyName: WriteCloudWatchLogs
43-
PolicyDocument:
44-
Version: '2012-10-17'
45-
Statement:
46-
- Effect: Allow
47-
Action:
48-
- 'logs:CreateLogGroup'
49-
- 'logs:CreateLogStream'
50-
- 'logs:PutLogEvents'
51-
Resource: 'arn:aws:logs:*:*:*'
52-
- PolicyName: TargetBucketAcces
53-
PolicyDocument:
54-
Version: '2012-10-17'
55-
Statement:
56-
- Effect: Allow
57-
Action:
58-
- 's3:ListBucket'
59-
Resource: !GetAtt TargetBucket.Arn
60-
- Effect: Allow
61-
Action:
62-
- "s3:PutObject"
63-
- "s3:GetObject"
64-
- "s3:DeleteObject"
65-
Resource: !Sub "${TargetBucket.Arn}/*"
66-
67-
LambdaFunction:
68-
Type: AWS::Lambda::Function
69-
Properties:
70-
Layers:
71-
- !Ref FrontendLayer
72-
Code:
73-
S3Bucket: DEPLOYMENT_BUCKET_NAME
74-
S3Key: DEPLOYMENT_KEY/function.zip
75-
Runtime: python3.6
76-
Handler: deployer.handler
77-
Role: !GetAtt LambdaRole.Arn
78-
Environment:
79-
Variables:
80-
TARGET_BUCKET: !Ref TargetBucket
81-
8233
Outputs:
83-
FrontendLayer:
84-
Value: !Ref FrontendLayer
85-
LambdaFunction:
86-
Value: !Ref LambdaFunction
34+
Arn:
35+
Value: !Ref S3DeploymentLayer

example/Makefile

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
BUCKET_NAME := desole-packaging
2+
STACK_NAME := s3-deployment-example-2
3+
4+
output.yml: template.yml src/*
5+
aws cloudformation package --template-file $< --output-template-file $@ --s3-bucket=$(BUCKET_NAME)
6+
7+
deploy: output.yml
8+
aws cloudformation deploy --template-file $< --stack-name $(STACK_NAME) --capabilities CAPABILITY_IAM
9+
@echo 'listing bucket'
10+
@aws cloudformation describe-stacks --stack-name $(STACK_NAME) --query Stacks[].Outputs[].OutputValue --output text
11+
@aws s3 ls s3://$(shell aws cloudformation describe-stacks --stack-name $(STACK_NAME) --query Stacks[].Outputs[].OutputValue --output text) --recursive

lambda/src/index.html renamed to example/src/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@
33

44
</head>
55
<body>
6-
<h1> HELLOOOO </h1>
6+
<h1> Test S3 Deployment </h1>
77
</body>
88
</html>

example/src/style.css

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
h1 {
2+
color: red;
3+
}

example/template.yml

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
AWSTemplateFormatVersion: 2010-09-09
2+
Transform: 'AWS::Serverless-2016-10-31'
3+
Description: Frontend Deploy Example
4+
5+
Resources:
6+
TargetBucket:
7+
Type: AWS::S3::Bucket
8+
9+
SiteSource:
10+
Type: AWS::Serverless::Function
11+
Properties:
12+
Layers:
13+
- arn:aws:lambda:us-east-1:145266761615:layer:s3-deployment:1
14+
CodeUri: src/
15+
AutoPublishAlias: live
16+
Runtime: python3.6
17+
Handler: deployer.resource_handler
18+
Policies:
19+
- S3CrudPolicy:
20+
BucketName: !Ref TargetBucket
21+
22+
DeploymentResource:
23+
Type: 'Custom::FrontendDeployment'
24+
Properties:
25+
ServiceToken: !GetAtt SiteSource.Arn
26+
TargetBucket: !Ref TargetBucket
27+
Acl: 'private'
28+
CacheControlMaxAge: 700
29+
Version: !Ref "SiteSource.Version"
30+
31+
Outputs:
32+
DestinationBucket:
33+
Value: !Ref TargetBucket

src/deployer.py

Lines changed: 44 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,28 @@
11
import os
22
import boto3
33
import mimetypes
4+
import json
5+
import requests
6+
47
s3 = boto3.resource('s3')
58

6-
def handler(event, context):
7-
target_bucket = os.environ['TARGET_BUCKET']
8-
lambda_src = os.getcwd()
9-
acl = 'private'
10-
cacheControl = 'max-age=600'
11-
12-
upload(lambda_src, target_bucket, acl, cacheControl)
9+
def resource_handler(event, context):
10+
print(event)
11+
try:
12+
target_bucket = event['ResourceProperties']['TargetBucket']
13+
lambda_src = os.getcwd()
14+
acl = event['ResourceProperties']['Acl']
15+
cacheControl = 'max-age=' + event['ResourceProperties']['CacheControlMaxAge']
16+
print(event['RequestType'])
17+
if event['RequestType'] == 'Create' or event['RequestType'] == 'Update':
18+
print('uploading')
19+
upload(lambda_src, target_bucket, acl, cacheControl)
20+
else:
21+
print('ignoring')
22+
23+
send_result(event)
24+
except Exception as err:
25+
send_error(event, err)
1326
return event
1427

1528
def upload(lambda_src, target_bucket, acl, cacheControl):
@@ -22,4 +35,27 @@ def upload(lambda_src, target_bucket, acl, cacheControl):
2235

2336
def upload_file(source, bucket, key, s3lib, acl, cacheControl, contentType):
2437
print('uploading from {} {} {}'.format(source, bucket, key))
25-
s3lib.Object(bucket, key).put(ACL=acl,Body=open(source, 'rb'),CacheControl=cacheControl,ContentType=contentType)
38+
s3lib.Object(bucket, key).put(ACL=acl,Body=open(source, 'rb'),CacheControl=cacheControl,ContentType=contentType)
39+
40+
def send_result(event):
41+
response_body = json.dumps({
42+
'Status': 'SUCCESS',
43+
'PhysicalResourceId': 'zeka123',
44+
'StackId': event['StackId'],
45+
'RequestId': event['RequestId'],
46+
'LogicalResourceId': event['LogicalResourceId']
47+
})
48+
print(response_body)
49+
requests.put(event['ResponseURL'], data = response_body)
50+
51+
def send_error(event, error):
52+
response_body = json.dumps({
53+
'Status': 'FAILED',
54+
'Reason': str(error),
55+
'PhysicalResourceId': event['PhysicalResourceId'] or event['RequestId'],
56+
'StackId': event['StackId'],
57+
'RequestId': event['RequestId'],
58+
'LogicalResourceId': event['LogicalResourceId']
59+
})
60+
print(response_body)
61+
requests.put(event['ResponseURL'], data = response_body)

0 commit comments

Comments
 (0)