Skip to main content

Search the Dev Center

Search tutorials, templates, examples, and more.

Serverless app to copy and zip objects between Amazon S3 buckets

Copy and zip TPS reports, using AWS Lambda, from one S3 Bucket to another.

This example lives in the pulumi/examples repository. Check out just this directory to use it:

Get started with this example
git clone --filter=blob:none --sparse https://github.com/pulumi/examples pulumi-examples
git -C pulumi-examples sparse-checkout set aws-ts-s3-lambda-copyzip
cd pulumi-examples/aws-ts-s3-lambda-copyzip

This example sets up two AWS S3 Buckets and a single Lambda that listens to one and, upon each new object arriving in it, zips it up and copies it to the second bucket. Its architecture looks like this:

Architecture

This example is also featured in the blog post Easy Serverless Apps and Infrastructure — Real Events, Real Code.

Prerequisites#

  1. Install Pulumi
  2. Configure AWS credentials
  3. Install Node.js

Deploying the example#

  1. Create a new stack, which is an isolated deployment target for this example:

    Terminal window
    pulumi stack init dev
  2. Set the AWS region to deploy into:

    Terminal window
    pulumi config set aws:region us-east-1
  3. Install dependencies:

    Terminal window
    npm install
  4. Deploy the stack:

    Terminal window
    pulumi up
  5. After about 20 seconds, your buckets and lambda will have been deployed. Their names are printed:

    Outputs:
    tpsReportsBucket: "tpsreports-21b7b7a"
    tpsZipsBucket : "tpszips-c869600"
  6. Copy a file to the tpsReportsBucket using the AWS CLI:

    Terminal window
    aws s3 cp ./myTpsReport001.txt s3://$(pulumi stack output tpsReportsBucket)
  7. Tail the logs to see evidence the Lambda ran:

    Terminal window
    pulumi logs -f
    Collecting logs for stack dev since 2019-03-10T10:09:56.000-07:00...
    2019-03-10T11:10:48.617-07:00[zipTpsReports] Zipping
    tpsreports-96458ef/tps001.txt into tpszips-edfde11/tps001.txt.zip
  8. Press ^C to exit pulumi logs -f, and then download your new zipfile:

    Terminal window
    aws s3 cp s3://$(pulumi stack output tpsZipsBucket)/myTpsReport001.txt.zip .

Cleaning up#

Once you’re done, delete the bucket contents (by default, bucket content isn’t auto-deleted), then destroy your stack and remove it:

Terminal window
aws s3 rm s3://$(pulumi stack output tpsReportsBucket)/myTpsReport001.txt
aws s3 rm s3://$(pulumi stack output tpsZipsBucket)/myTpsReport001.txt.zip
pulumi destroy
pulumi stack rm

Related

The infrastructure as code platform for any cloud.