aws-cdk icon indicating copy to clipboard operation
aws-cdk copied to clipboard

(cloudfront): EdgeFunction does not get permission to read from an S3 bucket

Open mgilroy opened this issue 5 years ago • 2 comments

What is the problem?

I am using cloudfront.experimental.EdgeFunction to provide a lambda function to allow a Angular based SPA to be served via cloudfront with a different application based upon the browsers language settings. I am using OriginAccessIdentiy to ensure that the S3 bucket does not need to be publicly available. However, the only way I can get the lambda to access the S3 bucket files is to make the bucket publicly accessible. When this is done, the lambda successfully returns the correct files without issue.

I'm therefore convinced that this is a permissions issue. I've attempted to manually assign permissions to the bucket in the console but to no avail. I can't see where else permissions changes would be required to make this just work.

Reproduction Steps

const siteBucket = new s3.Bucket(this, "18n-test-domain-com", {
  removalPolicy: cdk.RemovalPolicy.DESTROY,
  websiteIndexDocument: "index.html",
  websiteErrorDocument: "index.html"
});

const cloudFrontOAI = new cloudfront.OriginAccessIdentity(this, props.r3checks.envName('OAI'), {
  comment: `OAI for ${siteDomain} website.`,
});

const edgeLambda = new cloudfront.experimental.EdgeFunction(this, props.r3checks.envName("lambda-edge"), {
  runtime: lambda.Runtime.NODEJS_14_X,
  handler: "index.handler",
  code: lambda.Code.fromAsset(path.join(__dirname, "lambda")),
  logRetention: RetentionDays.ONE_DAY
});


const lambdaFunctionAssociations: cloudfront.EdgeLambda[] = [
  {
    functionVersion: edgeLambda.currentVersion,
    eventType: cloudfront.LambdaEdgeEventType.ORIGIN_REQUEST,
    includeBody: true
  }];


siteBucket.grantRead(edgeLambda);


const distribution = new cloudfront.Distribution(this,

    props.r3checks.envName("SiteDistribution"),
    {
      certificate: Certificate.fromCertificateArn(this, "ICertificate", certificateArn),
      defaultBehavior: {
        origin: new origins.S3Origin(siteBucket, {originAccessIdentity: cloudFrontOAI}),
        edgeLambdas: lambdaFunctionAssociations,
      },
      domainNames: [siteDomain],
      errorResponses: [{ responseHttpStatus: 200, responsePagePath: '/', httpStatus: 404 }],
      minimumProtocolVersion: SecurityPolicyProtocol.TLS_V1_2_2019
    }
);

What did you expect to happen?

I expected that when browsing to the site that a redirect based on language selection in the lambda would occur and site would appear.

What actually happened?

Language based redirection works, however, the lambda doesn't have permission to read the bucket and nothing is displayed.

CDK CLI Version

1.132.0

Framework Version

No response

Node.js Version

14.17.0

OS

linux

Language

Typescript

Language Version

3.9.7

Other information

No response

mgilroy avatar Nov 17 '21 11:11 mgilroy

I am using OriginAccessIdentiy to ensure that the S3 bucket does not need to be publicly available.

This isn't doing what you think it's doing. This is possibly part of the cause of the issue here. From the docs:

The above will treat the bucket differently based on if IBucket.isWebsite is set or not. If the bucket is configured as a website, the bucket is treated as an HTTP origin, and the built-in S3 redirects and error pages can be used. Otherwise, the bucket is handled as a bucket origin and CloudFront's redirect and error handling will be used. In the latter case, the Origin will create an origin access identity and grant it access to the underlying bucket. This can be used in conjunction with a bucket that is not public to require that your users access your content using CloudFront URLs and not S3 URLs directly.

You've configured your bucket as a website, which means the S3Origin is treating it as such and using the HTTP endpoint as the origin point. However, this does require that the bucket allow public access. The alternative is to remove the website configuration from the bucket, and use CloudFront's defaultRootObject and errorResponses, respectively to replace that functionality. See https://github.com/aws/aws-cdk/issues/17153#issuecomment-977924798 for a previous discussion of the same idea.

Closing this out. Feel free to comment and mention me if you have follow-ups.

njlynch avatar Dec 08 '21 16:12 njlynch

⚠️COMMENT VISIBILITY WARNING⚠️

Comments on closed issues are hard for our team to see. If you need more assistance, please either tag a team member or open a new issue that references this one. If you wish to keep having a conversation with other community members under this issue feel free to do so.

github-actions[bot] avatar Dec 08 '21 16:12 github-actions[bot]