-
Notifications
You must be signed in to change notification settings - Fork 0
/
layer_build.py
57 lines (54 loc) · 1.86 KB
/
layer_build.py
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
#!/usr/bin/env python
"""
Script to generate the CFN template for the library into a Lambda Layer from within the CodeBuild of the Layer Build.
"""
from datetime import datetime as dt
from os import environ
from json import dumps
from argparse import ArgumentParser
from ozone.templates.awslambdalayer import template
import boto3
def get_artifact_location():
"""
Retrieves the Destination bucket and path of the Layer from CodeBuild within the job
"""
job_id = environ['CODEBUILD_BUILD_ID']
client = boto3.client('codebuild')
build_info = client.batch_get_builds(
ids=[job_id]
)['builds'][0]
location = build_info['artifacts']['location'].strip('aws:arn:s3:::')
bucket = location.split('/')[0]
key = location.split('/', 1)[-1]
return (bucket, key)
if __name__ == '__main__':
PARSER = ArgumentParser('Codebuild CFN template and params build')
PARSER.add_argument(
'--path', help='Path where CFN files are created', required=True
)
ARGS = PARSER.parse_args()
BUILD_DEST = get_artifact_location()
LAYER_NAME = environ['LAYER_NAME']
PY_VERSION = environ['PY_VERSION']
DATE = dt.utcnow().isoformat()
TPL = template(make_public=True, Runtimes=[PY_VERSION], Bucket=BUILD_DEST[0], Key=BUILD_DEST[1])
TPL.set_metadata({
'Author': 'John Mille [email protected]',
'Version': DATE,
'BuildBy': 'CodePipeline/CodeBuild',
'LayerName': LAYER_NAME
})
TPL.set_description(f'Template for {LAYER_NAME} - {DATE}')
with open(f'{ARGS.path}/layer_template.yml', 'w') as fd:
fd.write(TPL.to_yaml())
template_config = {
'Parameters':
{
'LayerName': LAYER_NAME
},
'Tags': {
'Name': LAYER_NAME
}
}
with open(f'{ARGS.path}/layer_config.json', 'w') as fd:
fd.write(dumps(template_config))