3
votes

I have a Lambda Edge attached to a CloudFront distribution. What I want to do is use Serverless Framework to publish the lambda (instead of manually uploading files and click on "Deploy to Lambda@Edge"). What I've tried to do, looking at the serverless documentation, is add this yml file to the project and run the deployment script

service: cloudfront-service

provider:
  name: aws
  runtime: nodejs10.x

functions:
  cfLambda:
    handler: index.handler
    events:
      - cloudFront:
        eventType: origin-request
        origin: <CloudFront-Origin-ID>

This deployed the Lambda but it didn't attached it to CloudFront (it hasn't been published and there is no versions or triggers related). So how can I do this, using an existing CloudFront distribution?

3
don't know Serverless but if you are working with AWS why not use Cloudformation ? Their documentation is quite goodhazirovich
Idk, the dev who wrote the Lambda for some reason decide not to do this, leading now to this kinds of problems. By the way Serverless does exactly this, it creates a CloudFormation stack with the Lambda and the resources you specify in it's config file (for instance I used it to deploy a Lambda function that is attached to an API Gateway. Just set the required arguments in the yaml config file and everything has been created for me. I highly recommend you to check out this framework ;) )Jimi
Agreed, please disregard @hazirovich's commentsolsglasses
Don't know if this is relevant when using serverless, but as far as I know CloudFront does not yet support nodejs12.x for lambda@edge.nriedlin
Yes I found it out, but is unrelated to the issue since it doesn't work with Node10 too :/Jimi

3 Answers

1
votes

This plugin @silvermine/serverless-plugin-cloudfront-lambda-edge will not help if you want to use an existing cloud front distribution. It is only helpful if you are going to create a new one. This issue has been already reported and as per the forum, this functionality they are not supporting.

0
votes

Lambda@Edge with Serverless-Framework is quite easy. We use this plugin.

plugins:
   - '@silvermine/serverless-plugin-cloudfront-lambda-edge'

Please go directly to the plugin author's website for complete examples: https://github.com/silvermine/serverless-plugin-cloudfront-lambda-edge

0
votes

Base on your implementation you have a wrong indentation so I think it wont really attach it to your cloudfront. Having a wrong indetation will not create an events on your lambda function so intead of this

events:
      - cloudFront:
        eventType: origin-request
        origin: <CloudFront-Origin-ID>

Do this:

events:
      - cloudFront:
          eventType: origin-request
          origin: <CloudFront-Origin-ID>

I hope that this will solve your problem. Because I encounter this wrong indentation myself and wander why it is not being implemented properly.