sparse-intern-71089
03/16/2023, 4:30 PMbrave-planet-10645
03/16/2023, 4:50 PMbrave-planet-10645
03/16/2023, 5:11 PMsourceCodeHash
input of the layer. If you use the Pulumi aws.s3.BucketObject
resource, then this emits a source code hash that you can use. However, it seems that even without the code changing, the source code hash output keeps changing so you’ll get a new layer version every time you run an update.
2. The other way to do this is by versioning the S3 bucket and then passing the versionId
output from the bucket object into the s3ObjectVersion
input of the lambda layer.
If you choose to use method 2, you’ll end up with something like this:
import * as pulumi from "@pulumi/pulumi";
import * as aws from "@pulumi/aws";
const bucket = new aws.s3.Bucket("lambdaBucket", {
versioning: {
enabled: true
}
});
const bucketObject = new aws.s3.BucketObject("lambdacode", {
source: new pulumi.asset.AssetArchive({
".": new pulumi.asset.FileArchive("./lambda")
}),
bucket: bucket
});
const lambdaLayer = new aws.lambda.LayerVersion("layer", {
layerName: "piers-test",
s3Bucket: bucket.bucket,
s3Key: bucketObject.key,
s3ObjectVersion: bucketObject.versionId,
});
careful-summer-45848
03/16/2023, 6:42 PM#!/usr/bin/env tsx
import { $ } from "zx";
import fs from "fs";
import path from "path";
import crypto from "crypto";
import {
getChildDirectories,
srcLayerPath,
hashFilePath
} from "@lambda/script/helper";
import { rootDir } from "@root/root-dir";
async function updateHashes() {
const output = await $`git diff --name-only HEAD~1`;
const updated = output.stdout.split("\n")
const layers = getChildDirectories(srcLayerPath);
const hashFile = fs.readFileSync(hashFilePath, "utf8");
const hashes = JSON.parse(hashFile);
for (const layer of layers) {
const relPath = path.relative(rootDir, `${srcLayerPath}/${layer}`);
if (updated.find((file) => file.startsWith(relPath))) {
hashes[layer] = createHash();
}
}
fs.writeFileSync(hashFilePath, JSON.stringify(hashes));
}
function createHash() {
return crypto.createHash("sha256").update(Date.now().toString()).digest("base64");
}
updateHashes();
careful-summer-45848
03/16/2023, 7:01 PMNo matter how you like to participate in developer communities, Pulumi wants to meet you there. If you want to meet other Pulumi users to share use-cases and best practices, contribute code or documentation, see us at an event, or just tell a story about something cool you did with Pulumi, you are part of our community.
Powered by