Hopefully abstracting all the lambda stuff so I ca...
# general
e
Hopefully abstracting all the lambda stuff so I can just deal with coding the way requests are handled
w
We have a few options for this: 1. Using raw AWS APIs: https://github.com/pulumi/examples/tree/master/aws-ts-serverless-raw 2. Using our
aws.apigateway.x.API
to simplify construction of API Gateway: https://github.com/pulumi/examples/blob/master/aws-ts-apigateway/index.ts 3. Using our
@pulumi/cloud
package to do this in a cloud-neutral way: https://github.com/pulumi/examples/blob/master/cloud-js-api/index.js I'd suggest starting with (2), and referring to (1) if you need to tweak a lot of things or want to build everything up yourself. Note that the example in (2) uses a
Lambda
provided as a callback inline, but you can also just pass a
new aws.lambda.Function
you create yourself if you'd like.
e
Is it possible to do an S3
listObjects
with
@pulumi/aws
?
I'm trying to get a filtered result set listing objects in a bucket matching a wildcard name pattern
w
Do you want to do the
listObjects
during deployment? Or inside of a
Lambda
that runs at runtime?
e
inside the Lambda at runtime
which also implies setting the proper policies for the lambda role to access S3 - which will be my followup question
My basic goal is this: using #2 from above, I want to create a simple REST api running on lambda that will return a list of objects in an S3 bucket
bonus points if I can filter that list based on some wildcard pattern
w
Here's basically what you are describing I think:
Copy code
import * as aws from "@pulumi/aws";

let bucket = new aws.s3.Bucket("mybucket");
let object = new aws.s3.BucketObject("myobject", { bucket: bucket, key: "a", content: "hello" });
let endpoint = new aws.apigateway.x.API("hello-world", {
    routes: [
        { path: "/", method: "GET", eventHandler: async (event) => {
            const s3 = new aws.sdk.S3();
            const objects = await s3.listObjects({ Bucket: bucket.id.get() }).promise();
            return {
                statusCode: 200,
                body: JSON.stringify(objects),
            }
        },
    }],
});

export let url = endpoint.url;
And then:
Copy code
$ curl $(pulumi stack output url)
{"IsTruncated":false,"Marker":"","Contents":[{"Key":"a","LastModified":"2018-11-22T00:05:49.000Z","ETag":"\"5d41402abc4b2a76b9719d911017c592\"","Size":5,"StorageClass":"STANDARD","Owner":{"DisplayName":"joe","ID":"244d02f41fe32cd728a7d20986e102c9b2f626516b8cfe9477a0b7089765463f"}}],"Name":"mybucket-db4f5d2","Prefix":"","MaxKeys":1000,"CommonPrefixes":[]}
e
cool! Instead of creating an S3 bucket, can I get an existing one?
sorry, n/m misread it
w
You could pass
Prefix:
to `listObjects above to apply a prefix. And could compute that prefix from
event.queryStringParameters
,
event.body
or
event.pathParameters
if needed.
e
awesome, that totally works! Thanks for the sample
w
Instead of creating an S3 bucket, can I get an existing one?
Yep -
let bucket = aws.s3.Bucket.get("mybucket", "<http://www-pulumi-com.chris.moolumi.io|www-pulumi-com.chris.moolumi.io>")
. Where the second arg is the name of the bucket.