https://pulumi.com logo
Title
w

wet-ambulance-3300

02/19/2021, 10:17 AM
Is there a pulumi way to do the following? As in zip a file/directory and get the md5 hash like this.
data "archive_file" "source_archive" {
  source_dir  = var.source_directory
  output_path = "./main.zip"
  type        = "zip"
}

resource "google_storage_bucket_object" "archive" {
  name   = "${lower(replace(base64encode(data.archive_file.source_archive.output_md5), "=", ""))}.zip"
  bucket = google_storage_bucket.source_bucket.name
  source = data.archive_file.source_archive.output_path
}
I saw https://www.pulumi.com/docs/intro/concepts/assets-archives/#archives but as far as I can tell, it just represents an archive. It does not actually create the archive... Please correct me if i'm wrong.
g

gentle-diamond-70147

02/19/2021, 3:11 PM
AssetArchive will create the zip file for you.
w

wet-ambulance-3300

02/19/2021, 5:01 PM
Many thanks @gentle-diamond-70147. Could you recommend a library or method to do the md5 hash?
Hi @gentle-diamond-70147, I landed on this in the end.
import * as SparkMD5 from 'spark-md5'
import * as pulumi from '@pulumi/pulumi'
import * as gcp from '@pulumi/gcp'

        const source_archive = new pulumi.asset.FileArchive(args.source_directory)

      
        const source_object = new gcp.storage.BucketObject('source_object', {
            name: pulumi.output(source_archive.path).apply(file => `${SparkMD5.hash(file)}.zip`),
            bucket: source_bucket.name,
            source: source_archive.path
        })
g

gentle-diamond-70147

02/22/2021, 5:35 PM
Looks great!