The way to reference apig event url from resources

Hi
I need to design an image resizing service that is able to resize the image on the fly.

So the flow is as follow:

  1. Users send the request for image AAA.png in size 400*400 to S3 bucket
  2. If object isn’t there, S3 will then forward the request to lambda
  3. Lambda fetches the original image AAA.png from bucket and resize it, put it back into s3 as AAA_400_400.png
  4. Once lambda has done its job, the API Gateway permanently redirects the user to the AAA_400_400.png stored in S3.

The place I get stuck is S3 redirect the request to lambda
Wonder how I can reference the apig hostname inside my s3 resource.

See below

Thanks

You can reference it directly { "Ref" : "ApiGatewayRestApi" }.

See this question I asked before. [Fixed] How do i get/reference api gateway restapi id in serverless.yml?

This is the way to get api gateway id. You need use it and add more path (region, etc) to form the full api gateway url

Thanks mate for pointing me at the correct path.

One more quick question, it appears that ApiGatewayRestApi corresponds to AWS::ApiGateway::RestApi and in aws docs, it says

When the logical ID of this resource is provided to the Ref intrinsic function, Ref returns the RestApi ID, such as a1bcdef2gh.

So I assume ApiGatewayRestApi is the logical id? But I could not find ApiGatewayRestApi anywhere in serverless docs.

I wrote something on this a year ago http://www.goingserverless.com/blog/api-gateway-url. The short version is:

{ "Fn::Join" : ["", [ "https://", { "Ref" : "ApiGatewayRestApi" }, ".execute-api.${self:custom.region}.amazonaws.com/${self:custom.stage}" ] ]  }
1 Like

Yup. I’ve read your post. Really helpful! Thanks