0
votes

I am associating the lambda functions in the AWS Cloudfront. While associating the function in behavior patterns of web distribution of cloudfront, I am getting the below error:

com.amazonaws.services.cloudfront.model.InvalidLambdaFunctionAssociationException: The function has an invalid runtime for functions that are triggered by a CloudFront event: python2.7 Expecting: nodejs6.10 Function: arn:aws:lambda:us-east-1:316211033416:function:domain_redirects:1 (Service: AmazonCloudFront; Status Code: 400; Error Code: InvalidLambdaFunctionAssociation; Request ID: 8771bb2b-3b50-11e8-a02b-47ca03858fe8)

python lambda function:

from __future__ import print_function

import json

print('Loading function')


def domain_redirects(event, context):
    #print("Received event: " + json.dumps(event, indent=2))
    request=event['Records'][0]['cf']['request']
    headers=request['headers']
    mob_device=["android","iphone","googlebot-mobile"]
    user_agent=headers['user-agent'][0]['value'].lower()
    for device in mob_device:
        if device in user_agent:
            response = {
                'status':'302',
                'statusDescription':'Found',
                'headers': {
                    'location': [
                    {
                    'key': 'Location',
                    'value': 'http://m.bakewish.in'
                    }
                    ],
                }
            }
            # print (device + "detected")
            return response

The role which has created the lambda function has below trust relationship:

{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Allow",
      "Principal": {
        "Service": [
          "lambda.amazonaws.com",
          "edgelambda.amazonaws.com"
        ]
      },
      "Action": "sts:AssumeRole"
    }
  ]
}

and below permissions:

1) AWSLambdaEdgeExecutionRole 2) CloudfrontFullAccess

Any help is appreciated!!

1
Thanks for the question, I was getting mixed up on the Policy Stuff.user3888307

1 Answers

3
votes

Looking at the documentation it looks like you must use nodejs6.10 runtime for cloudfront to work with lambda.

You must create functions with the nodejs6.10 runtime property.