|
| 1 | +package airhacks.cloudfront.boundary; |
| 2 | + |
| 3 | +import airhacks.InfrastructureBuilder; |
| 4 | +import airhacks.functionurl.boundary.FunctionURLStack; |
| 5 | +import airhacks.lambda.control.QuarkusLambda; |
| 6 | +import software.amazon.awscdk.CfnOutput; |
| 7 | +import software.amazon.awscdk.Stack; |
| 8 | +import software.amazon.awscdk.services.cloudfront.AllowedMethods; |
| 9 | +import software.amazon.awscdk.services.cloudfront.BehaviorOptions; |
| 10 | +import software.amazon.awscdk.services.cloudfront.CachePolicy; |
| 11 | +import software.amazon.awscdk.services.cloudfront.Distribution; |
| 12 | +import software.amazon.awscdk.services.cloudfront.OriginProtocolPolicy; |
| 13 | +import software.amazon.awscdk.services.cloudfront.SSLMethod; |
| 14 | +import software.amazon.awscdk.services.cloudfront.SecurityPolicyProtocol; |
| 15 | +import software.amazon.awscdk.services.cloudfront.ViewerProtocolPolicy; |
| 16 | +import software.amazon.awscdk.services.cloudfront.origins.HttpOrigin; |
| 17 | +import software.amazon.awscdk.services.lambda.FunctionUrlAuthType; |
| 18 | +import software.amazon.awscdk.services.lambda.FunctionUrlOptions; |
| 19 | +import software.constructs.Construct; |
| 20 | + |
| 21 | +public class FunctionURLCloudFrontStack extends Stack { |
| 22 | + public static class FunctionURLBuilder { |
| 23 | + private InfrastructureBuilder infrastructureBuilder; |
| 24 | + private FunctionUrlAuthType authType = FunctionUrlAuthType.NONE; |
| 25 | + |
| 26 | + public FunctionURLBuilder(InfrastructureBuilder infrastructureBuilder) { |
| 27 | + this.infrastructureBuilder = infrastructureBuilder; |
| 28 | + } |
| 29 | + |
| 30 | + public Construct construct() { |
| 31 | + return this.infrastructureBuilder.construct(); |
| 32 | + } |
| 33 | + |
| 34 | + public String stackId() { |
| 35 | + return this.infrastructureBuilder.stackId(); |
| 36 | + } |
| 37 | + |
| 38 | + public FunctionURLCloudFrontStack build() { |
| 39 | + return new FunctionURLCloudFrontStack(this); |
| 40 | + } |
| 41 | + |
| 42 | + } |
| 43 | + |
| 44 | + public FunctionURLCloudFrontStack(FunctionURLBuilder builder) { |
| 45 | + super(builder.construct(), builder.stackId()); |
| 46 | + var infrastructureBuilder = builder.infrastructureBuilder; |
| 47 | + var quarkusLambda = new QuarkusLambda(this, infrastructureBuilder.functionZipLocation(), |
| 48 | + infrastructureBuilder.functionName(), |
| 49 | + infrastructureBuilder.functionHandler(), infrastructureBuilder.ram(), |
| 50 | + infrastructureBuilder.isSnapStart(), |
| 51 | + infrastructureBuilder.timeout(), |
| 52 | + infrastructureBuilder.configuration()); |
| 53 | + var function = quarkusLambda.getFunction(); |
| 54 | + var functionUrl = function.addFunctionUrl(FunctionUrlOptions.builder() |
| 55 | + .authType(builder.authType) |
| 56 | + .build()); |
| 57 | + var distribution = Distribution.Builder.create(this, "FunctionURLDistribution") |
| 58 | + .minimumProtocolVersion(SecurityPolicyProtocol.SSL_V3) |
| 59 | + .defaultBehavior(BehaviorOptions.builder() |
| 60 | + .origin(new HttpOrigin(functionUrl.getUrl())) |
| 61 | + .allowedMethods(AllowedMethods.ALLOW_ALL) |
| 62 | + .viewerProtocolPolicy(ViewerProtocolPolicy.REDIRECT_TO_HTTPS) |
| 63 | + .cachePolicy(CachePolicy.CACHING_DISABLED) |
| 64 | + .build()) |
| 65 | + .build(); |
| 66 | + CfnOutput.Builder.create(this, "CloudFrontDistributionDomainNameOutput").value(distribution.getDistributionDomainName()).build(); |
| 67 | + |
| 68 | + } |
| 69 | + |
| 70 | +} |
0 commit comments