Skip to content

Commit a19aef1

Browse files
committed
added initial string substitution
1 parent 48d458d commit a19aef1

File tree

6 files changed

+63
-9
lines changed

6 files changed

+63
-9
lines changed

Makefile

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
DEPLOYMENT_BUCKET_NAME := desole-packaging
22
DEPLOYMENT_KEY := $(shell echo s3-deployment-$$RANDOM.zip)
3-
STACK_NAME := s3-deployment
3+
STACK_NAME ?= s3-deployment
44

55
clean:
66
rm -rf build
@@ -19,5 +19,5 @@ build/layer.zip: build/python/deployer.py build/python/requests
1919

2020
deploy: cloudformation/template.yml build/layer.zip
2121
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)
23-
aws cloudformation describe-stacks --stack-name $(STACK_NAME) --query Stacks[].Outputs[].OutputValue --output text
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) LayerName=$(STACK_NAME)
23+
aws cloudformation describe-stacks --stack-name $(STACK_NAME) --query Stacks[].Outputs[].OutputValue --output text

cloudformation/template.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ Resources:
1313
CompatibleRuntimes:
1414
- python3.6
1515
Description: S3 Deployment Layer
16-
LayerName: s3-deployment
16+
LayerName: !Ref AWS::StackName
1717
LicenseInfo: MIT
1818
Content:
1919
S3Bucket: !Ref DeploymentBucketName
@@ -28,4 +28,4 @@ Resources:
2828

2929
Outputs:
3030
Arn:
31-
Value: !Ref S3DeploymentLayer
31+
Value: !Ref S3DeploymentLayer

example/template.yml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ Resources:
1616
Layers:
1717
# the layer contains the deployment code
1818
# so the function "source" can just contain the web assets
19-
- arn:aws:lambda:us-east-1:145266761615:layer:s3-deployment:4
19+
- arn:aws:lambda:us-east-1:145266761615:layer:test-s3-deployment:8
2020

2121
# point to directory with the assets so cloudformation can
2222
# package and upload them
@@ -56,6 +56,12 @@ Resources:
5656

5757
# tell the deployer where to upload the files
5858
TargetBucket: !Ref TargetBucket
59+
60+
Substitutions:
61+
FilePattern: "*.html"
62+
Values:
63+
APP_NAME: 'Example Application'
64+
STACK_ID: !Ref AWS::StackId
5965

6066
# Choose the ACL and caching policies
6167
# eg, for directly accessible web site

example/web-site/index.html

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
<html>
22
<head>
3-
<title>Example Website</title>
3+
<title>${APP_NAME}</title>
44
</head>
55
<body>
66
<h1> This is your website that is deployable and publishable on AWS Serverless Application Repository </h1>
7+
<h2> Your Stack ID is ${STACK_ID} </h2>
8+
9+
<script src="main.js"></script>
710
</body>
8-
</html>
11+
</html>

example/web-site/main.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
function testFunction(){
2+
console.log('hello')
3+
}

src/deployer.py

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import mimetypes
44
import json
55
import requests
6+
import subprocess
7+
import tempfile
68

79
s3 = boto3.resource('s3')
810

@@ -16,6 +18,10 @@ def resource_handler(event, context):
1618
event['ResourceProperties']['CacheControlMaxAge']
1719
print(event['RequestType'])
1820
if event['RequestType'] == 'Create' or event['RequestType'] == 'Update':
21+
22+
if 'Substitutions' in event['ResourceProperties'].keys():
23+
lambda_src = apply_substitutions(event['ResourceProperties']['Substitutions'])
24+
1925
print('uploading')
2026
upload(lambda_src, target_bucket, acl, cacheControl)
2127
else:
@@ -71,4 +77,40 @@ def get_physical_resource_id(event):
7177
if 'PhysicalResourceId' in event.keys():
7278
return event['PhysicalResourceId']
7379
else:
74-
return event['RequestId']
80+
return event['RequestId']
81+
82+
def apply_substitutions(substitutions):
83+
if not 'Values' in substitutions.keys():
84+
raise ValueError('Substitutions must contain Values')
85+
86+
if not isinstance(substitutions['Values'], dict):
87+
raise ValueError('Substitutions.Values must be an Object')
88+
89+
if len(substitutions['Values']) < 1:
90+
raise ValueError('Substitutions.Values must not be empty')
91+
92+
if not 'FilePattern' in substitutions.keys():
93+
raise ValueError('Substitutions must contain FilePattern')
94+
95+
if not isinstance(substitutions['FilePattern'], str):
96+
raise ValueError('Substitutions.FilePattern must be a String')
97+
98+
if len(substitutions['FilePattern']) < 1:
99+
raise ValueError('Substitutions.FilePattern must not be empty')
100+
101+
values = substitutions['Values']
102+
file_pattern = substitutions['FilePattern']
103+
104+
temp_dir = tempfile.mkdtemp()
105+
sub_dir = os.path.join(temp_dir, 'src')
106+
subprocess.run(['cp', '-r', os.getcwd(), sub_dir])
107+
108+
full_path = os.path.join(sub_dir, 'index.html')
109+
sed_script = ';'.join(list(map(lambda key: str.format('s/{}/{}/g', sed_escape(key), sed_escape(values[key])), values.keys())))
110+
print('sed script', sed_script)
111+
subprocess.run(['sed', sed_script, '-i', full_path], cwd=tempfile.gettempdir(), check=True)
112+
113+
return sub_dir
114+
115+
def sed_escape(text):
116+
return text.replace('/', '\\/')

0 commit comments

Comments
 (0)