MCPcopy Create free account
hub / github.com/PRX/Infrastructure / lambda_handler

Function lambda_handler

spire/cd/src/template-copy/lambda_function.py:31–98  ·  view source on GitHub ↗
(event, context)

Source from the content-addressed store, hash-verified

29
30
31def lambda_handler(event, context):
32 try:
33 print("Starting sync...")
34
35 job = event["CodePipeline.job"]
36
37 print("...Syncing repository source...")
38
39 if len(job["data"]["inputArtifacts"]) != 1:
40 raise Exception("Expected exactly 1 input artifact")
41
42 input_artifact = job["data"]["inputArtifacts"][0]
43
44 commit_hash = input_artifact["revision"]
45
46 input_location = input_artifact["location"]["s3Location"]
47 input_bucket = input_location["bucketName"]
48 input_key = input_location["objectKey"]
49 input_id = input_key.split("/")[-1]
50
51 archive_path = "/tmp/{0}".format(input_id)
52
53 print(f"...Getting artifact from {input_bucket}/{input_key}...")
54 print(f"...Writing artifact to {archive_path}...")
55
56 output_bucket_name = os.environ["TEMPLATE_COPY_BUCKET_NAME"]
57
58 s3.download_file(input_bucket, input_key, archive_path)
59
60 archive = zipfile.ZipFile(archive_path, "r")
61 names = archive.namelist()
62 for name in names:
63 # `name` will be the full relative file path for the file as it
64 # exists in the Infrastructure repository.
65 # For example:
66 # - "README.md"
67 # - "dns/prx.mx-hosted_zone.yml"
68 # - "spire/templates/root.yml"
69 if f"{name}".startswith("spire/templates"):
70 print(f"...Uploading {name}...")
71
72 f = archive.open(name)
73
74 # The object key will match the name from above, with a Git
75 # hash (and slash) prepended.
76 # For example:
77 # - "046c0da3e8cb4…ec83755ad111/README.md"
78 # - "046c0da3e8cb4…ec83755ad111/dns/prx.mx-hosted_zone.yml"
79 # - "046c0da3e8cb4…ec83755ad111/spire/templates/root.yml"
80 output_key = "{0}/{1}".format(commit_hash, name)
81 s3.upload_fileobj(f, output_bucket_name, output_key)
82
83 region = os.environ["AWS_REGION"]
84 template_url_base = (
85 f"https://{output_bucket_name}.s3.{region}.amazonaws.com/{commit_hash}"
86 )
87
88 code_pipeline.put_job_success_result(

Callers

nothing calls this directly

Calls 1

put_job_failureFunction · 0.85

Tested by

no test coverage detected