awsdocs / aws-cdk-guide

User guide for the AWS Cloud Development Kit (CDK).
Other
335 stars 222 forks source link

CloudFront with custom Custom Lambda Function #293

Closed rahejaps closed 3 years ago

rahejaps commented 3 years ago

I am trying to create CloudFront Distribution with EdgeLambda.

Tried to follow this issue [https://github.com/aws/aws-cdk/issues/6259] however, I kept getting an error

cdk ls
jsii.errors.JavaScriptError: 
  Error: cloudfront/version/Resource [AWS::Lambda::Version] is missing required property: functionName

Here is the code I am working with...

from aws_cdk import core
import aws_cdk.aws_cloudfront as cloudfront
import aws_cdk.aws_cloudfront_origins as origins
import aws_cdk.aws_iam as iamrole
import aws_cdk.aws_lambda as lambd 
import aws_cdk.custom_resources as custom

class cloudfrontStack(core.Stack):

    def __init__(self, scope: core.Construct, id: str, common, webstack, **kwargs) -> None:
        super().__init__(scope, id, **kwargs)

        # The code that defines your stack goes here
        lambdaedgefile = open("./bootstrap/lambdaedge.txt")
        lambdaedgecode = lambdaedgefile.read()

        #lamdaedgeversionfunction code
        lambdaedgeverfile = open("./bootstrap/lambdaedgeversion.txt")
        lambdaedgevercode = lambdaedgeverfile.read()

        lambdarole = "arn:aws:iam::6055:role/iam-lambda-edge-role"

        role = iamrole.Role.from_role_arn(self, "kmsrole", role_arn = lambdarole)

        lambdaedgefn = lambd.Function(
           self, 
           "lambdaEdgeFunction",
           description="A custom Lambda@Edge function for serving custom headers from CloudFront Distribution",
           function_name="lambda-edge",
           runtime=lambd.Runtime.NODEJS_12_X,
           role= role,
           memory_size=128,
           timeout=core.Duration.seconds(30),
           handler="index.handler",
           code=lambd.Code.inline(lambdaedgecode)
        )

        lambdaedgeversionfn = lambd.Function(
           self, 
           "lambdaEdgeVersionFunction",
           description="A custom Lambda@Edge Version function",
           function_name="lambda-edge-version",
           runtime=lambd.Runtime.NODEJS_10_X,
           role=role,
           memory_size=128,
           timeout=core.Duration.seconds(30),
           handler="index.handler",
           code=lambd.Code.inline(lambdaedgevercode)           
        )

        customlambdaedge = core.CustomResource(
            self,
            id = "lambdaedgeversion",
            service_token=lambdaedgeversionfn.function_arn,
            resource_type="Custom::LambdaVersion",
            properties={
                "FunctionName": lambdaedgefn.function_name,
                "LambdaCodeVersion" : "1"
            }
        )
        lambdaversion = lambd.Version(
            self,
            id = "version",
            lambda_=customlambdaedge         
        )
        lambdaversion.function_name("testfunctionname") ###### This did not work either

        alborigins = origins.LoadBalancerV2Origin(
            load_balancer= webstack.alb,
            https_port= 443,
            keepalive_timeout= core.Duration.seconds(60),
            origin_ssl_protocols= [ cloudfront.OriginSslPolicy.TLS_V1, cloudfront.OriginSslPolicy.TLS_V1_2,cloudfront.OriginSslPolicy.TLS_V1_1],
            protocol_policy= cloudfront.OriginProtocolPolicy.HTTPS_ONLY,
            read_timeout= core.Duration.seconds(60),
            )

        distribution = cloudfront.Distribution(
            self,
            id="cloudfrontdistribution",
            price_class= cloudfront.PriceClass.PRICE_CLASS_100,
            http_version =  cloudfront.HttpVersion.HTTP2,
            comment= "CloudFront Distribution",
            enabled= True,
            enable_ipv6= False,
            log_file_prefix= "cf",
            log_includes_cookies=False,

            default_behavior= cloudfront.BehaviorOptions(
                allowed_methods= cloudfront.AllowedMethods.ALLOW_ALL,
                cached_methods= cloudfront.CachedMethods.CACHE_GET_HEAD,
                viewer_protocol_policy=cloudfront.ViewerProtocolPolicy.REDIRECT_TO_HTTPS,
                origin= alborigins,
                edge_lambdas= [{
                    "functionVersion" : customlambdaedge,
                    "eventType": cloudfront.LambdaEdgeEventType.ORIGIN_RESPONSE,
                    "lambdaFunction" : lambdaversion #customlambdaedge.get_att_string('Output')
                }]
            ),
        )

Any guidance is appreciated

ghost commented 3 years ago

@rahejaps You should probably post this as a comment on the existing issue, or create a new issue in the CDK repo. This repo is for CDK Developer Guide issues and this content isn't in the Developer Guide.