Skip to content

Instantly share code, notes, and snippets.

@Dreamescaper
Created March 1, 2023 13:17
Show Gist options
  • Select an option

  • Save Dreamescaper/c0ecac849ce41eb8d8633185c6891e49 to your computer and use it in GitHub Desktop.

Select an option

Save Dreamescaper/c0ecac849ce41eb8d8633185c6891e49 to your computer and use it in GitHub Desktop.
dotnet lambda CDK Custom AssetHash
public static Code GetLambdaCode(string zipPath)
{
return Code.FromAsset(zipPath, new AssetOptions
{
AssetHash = HashForZipContent(zipPath),
AssetHashType = Amazon.CDK.AssetHashType.CUSTOM
});
}
private static string HashForZipContent(string zipPath)
{
// See https://github.com/aws/aws-extensions-for-dotnet-cli/issues/195
using var sha256 = SHA256.Create();
IEnumerable<byte> fileHashes = Array.Empty<byte>();
using var archive = ZipFile.OpenRead(zipPath);
foreach (var entry in archive.Entries.OrderBy(e => e.FullName))
{
using var entryStream = entry.Open();
var entryHash = sha256.ComputeHash(entryStream);
fileHashes = fileHashes.Concat(entryHash);
}
var zipHash = sha256.ComputeHash(fileHashes.ToArray());
return Convert.ToBase64String(zipHash);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment