Skip to content

Commit

Permalink
to private method
Browse files Browse the repository at this point in the history
  • Loading branch information
watany-dev committed Sep 10, 2024
1 parent f660a83 commit 2ad9170
Showing 1 changed file with 12 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -106,18 +106,12 @@ class FunctionUrlOriginWithOAC extends cloudfront.OriginBase {

public bind(scope: Construct, options: cloudfront.OriginBindOptions): cloudfront.OriginBindConfig {
const originBindConfig = super.bind(scope, options);
const distributionId = options.distributionId;

if (!this.originAccessControl) {
this.originAccessControl = new cloudfront.FunctionUrlOriginAccessControl(scope, 'FunctionUrlOriginAccessControl');
}

new lambda.CfnPermission(scope, `InvokeFromApiFor${options.originId}`, {
principal: 'cloudfront.amazonaws.com',
action: 'lambda:InvokeFunctionUrl',
functionName: cdk.Fn.select(6, cdk.Fn.split(':', this.functionUrl.functionArn)),
sourceArn: `arn:${cdk.Aws.PARTITION}:cloudfront::${cdk.Aws.ACCOUNT_ID}:distribution/${distributionId}`,
});
this.addInvokePermission(scope, options);

return {
...originBindConfig,
Expand All @@ -127,4 +121,15 @@ class FunctionUrlOriginWithOAC extends cloudfront.OriginBase {
},
};
}

private addInvokePermission(scope: Construct, options: cloudfront.OriginBindOptions) {
const distributionId = options.distributionId;

new lambda.CfnPermission(scope, `InvokeFromApiFor${options.originId}`, {
principal: 'cloudfront.amazonaws.com',
action: 'lambda:InvokeFunctionUrl',
functionName: cdk.Fn.select(6, cdk.Fn.split(':', this.functionUrl.functionArn)),
sourceArn: `arn:${cdk.Aws.PARTITION}:cloudfront::${cdk.Aws.ACCOUNT_ID}:distribution/${distributionId}`,
});
}
}

0 comments on commit 2ad9170

Please sign in to comment.