diff --git a/java/custom-resource/src/main/java/software/amazon/awscdk/examples/CustomResourceApp.java b/java/custom-resource/src/main/java/software/amazon/awscdk/examples/CustomResourceApp.java index 4dd78201..9392e0ac 100644 --- a/java/custom-resource/src/main/java/software/amazon/awscdk/examples/CustomResourceApp.java +++ b/java/custom-resource/src/main/java/software/amazon/awscdk/examples/CustomResourceApp.java @@ -3,11 +3,11 @@ import software.amazon.awscdk.core.App; public class CustomResourceApp { - public static void main(final String args[]) { - App app = new App(); - - new CustomResourceStack(app, "cdk-custom-resource-example2"); + public static void main(final String args[]) { + App app = new App(); - app.synth(); - } + new CustomResourceStack(app, "cdk-custom-resource-example2"); + + app.synth(); + } } diff --git a/java/custom-resource/src/main/java/software/amazon/awscdk/examples/CustomResourceStack.java b/java/custom-resource/src/main/java/software/amazon/awscdk/examples/CustomResourceStack.java index 517fd2e2..aff34474 100644 --- a/java/custom-resource/src/main/java/software/amazon/awscdk/examples/CustomResourceStack.java +++ b/java/custom-resource/src/main/java/software/amazon/awscdk/examples/CustomResourceStack.java @@ -1,5 +1,9 @@ package software.amazon.awscdk.examples; +import java.nio.file.*; +import java.util.HashMap; +import java.util.Map; +import java.util.UUID; import software.amazon.awscdk.core.CfnOutput; import software.amazon.awscdk.core.Construct; import software.amazon.awscdk.core.Duration; @@ -7,63 +11,57 @@ import software.amazon.awscdk.services.cloudformation.*; import software.amazon.awscdk.services.lambda.*; import software.amazon.awscdk.services.lambda.Runtime; -import java.util.HashMap; -import java.util.Map; -import java.util.UUID; -import java.nio.file.*; public class CustomResourceStack extends Stack { - public CustomResourceStack(final Construct scope, final String id) { - super(scope, id); - - try { + public CustomResourceStack(final Construct scope, final String id) { + super(scope, id); + + try { + + // Get the lambda function code + String LambdaContent = readFileAsString("./lambda/custom-resource-handler.py"); + + // Sample Lambda Function Resource + final SingletonFunction lambdaFunction = + SingletonFunction.Builder.create(this, "cdk-lambda-customresource") + .description("My Custom Resource Lambda") + .code(Code.fromInline(LambdaContent)) + .handler("index.handler") + .timeout(Duration.seconds(300)) + .runtime(Runtime.PYTHON_2_7) + .uuid(UUID.randomUUID().toString()) + .build(); - // Get the lambda function code - String LambdaContent = readFileAsString("./lambda/custom-resource-handler.py"); - - // Sample Lambda Function Resource - final SingletonFunction lambdaFunction = SingletonFunction.Builder.create(this, "cdk-lambda-customresource") - .description("My Custom Resource Lambda") - .code(Code.fromInline(LambdaContent)) - .handler("index.handler") - .timeout(Duration.seconds(300)) - .runtime(Runtime.PYTHON_2_7) - .uuid(UUID.randomUUID().toString()) - .build(); - - // Sample Property to send to Lambda Function - Map map = new HashMap(); - map.put("Message", "AWS CDK"); - - final CustomResource myCustomResource = CustomResource.Builder.create(this, "MyCustomResource") - .provider(CustomResourceProvider.fromLambda(lambdaFunction)) - .properties(map).build(); + // Sample Property to send to Lambda Function + Map map = new HashMap(); + map.put("Message", "AWS CDK"); - - // Publish the custom resource output - CfnOutput cfnoutput = CfnOutput.Builder.create(this, "MyCustomResourceOutput") - .description("The message that came back from the Custom Resource") - .value(myCustomResource.getAtt("Response").toString()).build(); - - - } catch (Exception e) { - e.printStackTrace(); - } - + final CustomResource myCustomResource = + CustomResource.Builder.create(this, "MyCustomResource") + .provider(CustomResourceProvider.fromLambda(lambdaFunction)) + .properties(map) + .build(); + + // Publish the custom resource output + CfnOutput cfnoutput = + CfnOutput.Builder.create(this, "MyCustomResourceOutput") + .description("The message that came back from the Custom Resource") + .value(myCustomResource.getAtt("Response").toString()) + .build(); + + } catch (Exception e) { + e.printStackTrace(); + } + } + // function to read the file content + public static String readFileAsString(String fileName) throws Exception { + String data = ""; + try { + data = new String(Files.readAllBytes(Paths.get(fileName)), "UTF-8"); + } catch (Exception e) { + e.printStackTrace(); } - // function to read the file content - public static String readFileAsString(String fileName) throws Exception - { - String data = ""; - try - { - data = new String(Files.readAllBytes(Paths.get(fileName)),"UTF-8"); - } - catch (Exception e) - { - e.printStackTrace(); - } - return data; - } + return data; + } } diff --git a/java/static-site/src/main/java/software/amazon/awscdk/examples/StaticSiteApp.java b/java/static-site/src/main/java/software/amazon/awscdk/examples/StaticSiteApp.java index f001ce3e..9c552876 100644 --- a/java/static-site/src/main/java/software/amazon/awscdk/examples/StaticSiteApp.java +++ b/java/static-site/src/main/java/software/amazon/awscdk/examples/StaticSiteApp.java @@ -1,52 +1,48 @@ package software.amazon.awscdk.examples; +import java.util.HashMap; +import java.util.Map; import software.amazon.awscdk.core.App; import software.amazon.awscdk.core.Environment; import software.amazon.awscdk.core.Stack; import software.amazon.awscdk.core.StackProps; -import java.util.HashMap; -import java.util.Map; public class StaticSiteApp extends Stack { - /** - * This stack relies on getting the domain name from CDK context. - * Use 'cdk synth -c domain=mystaticsite.com -c subdomain=www' - * Or add the following to cdk.json: - * { - * "context": { - * "domain": "mystaticsite.com", - * "subdomain": "www" - * } - * } - **/ - - public StaticSiteApp(App scope, String id, StackProps props) { - super(scope, id, props); - - // Getting domain and subdomain values from Context - - Map domainValues = new HashMap(); - domainValues.put("domainName", this.getNode().tryGetContext("domain")); - domainValues.put("siteSubDomain",this.getNode().tryGetContext("subdomain")); - - // Call StaticSiteStack to create the stack - new StaticSiteStack(this, "MyStaticSite", domainValues); - - } - public static void main(final String argv[]) { - - App app = new App(); - - // Stack must be in us-east-1, because the ACM certificate for a - // global CloudFront distribution must be requested in us-east-1. - StackProps pr = new StackProps.Builder().env(Environment.builder().region("us-east-1").account(System.getenv("CDK_DEFAULT_ACCOUNT")).build()) - .build(); - - new StaticSiteApp(app,"MyStaticSite", pr); - app.synth(); - - } + /** + * This stack relies on getting the domain name from CDK context. Use 'cdk synth -c + * domain=mystaticsite.com -c subdomain=www' Or add the following to cdk.json: { "context": { + * "domain": "mystaticsite.com", "subdomain": "www" } } + */ + public StaticSiteApp(App scope, String id, StackProps props) { + super(scope, id, props); + + // Getting domain and subdomain values from Context + + Map domainValues = new HashMap(); + domainValues.put("domainName", this.getNode().tryGetContext("domain")); + domainValues.put("siteSubDomain", this.getNode().tryGetContext("subdomain")); + + // Call StaticSiteStack to create the stack + new StaticSiteStack(this, "MyStaticSite", domainValues); + } + + public static void main(final String argv[]) { + + App app = new App(); + + // Stack must be in us-east-1, because the ACM certificate for a + // global CloudFront distribution must be requested in us-east-1. + StackProps pr = + new StackProps.Builder() + .env( + Environment.builder() + .region("us-east-1") + .account(System.getenv("CDK_DEFAULT_ACCOUNT")) + .build()) + .build(); + + new StaticSiteApp(app, "MyStaticSite", pr); + app.synth(); + } } - - diff --git a/java/static-site/src/main/java/software/amazon/awscdk/examples/StaticSiteStack.java b/java/static-site/src/main/java/software/amazon/awscdk/examples/StaticSiteStack.java index f984aad6..61d1a7af 100644 --- a/java/static-site/src/main/java/software/amazon/awscdk/examples/StaticSiteStack.java +++ b/java/static-site/src/main/java/software/amazon/awscdk/examples/StaticSiteStack.java @@ -1,132 +1,146 @@ package software.amazon.awscdk.examples; + import java.util.ArrayList; import java.util.List; import java.util.Map; - import software.amazon.awscdk.core.CfnOutput; import software.amazon.awscdk.core.Construct; import software.amazon.awscdk.core.RemovalPolicy; +import software.amazon.awscdk.services.certificatemanager.DnsValidatedCertificate; import software.amazon.awscdk.services.cloudfront.AliasConfiguration; import software.amazon.awscdk.services.cloudfront.Behavior; import software.amazon.awscdk.services.cloudfront.CloudFrontWebDistribution; import software.amazon.awscdk.services.cloudfront.S3OriginConfig; import software.amazon.awscdk.services.cloudfront.SSLMethod; -import software.amazon.awscdk.services.cloudfront.SourceConfiguration; import software.amazon.awscdk.services.cloudfront.SecurityPolicyProtocol; -import software.amazon.awscdk.services.s3.Bucket; -import software.amazon.awscdk.services.route53.RecordTarget; +import software.amazon.awscdk.services.cloudfront.SourceConfiguration; import software.amazon.awscdk.services.route53.ARecord; -import software.amazon.awscdk.services.route53.targets.CloudFrontTarget; import software.amazon.awscdk.services.route53.HostedZone; import software.amazon.awscdk.services.route53.HostedZoneProviderProps; import software.amazon.awscdk.services.route53.IHostedZone; +import software.amazon.awscdk.services.route53.RecordTarget; +import software.amazon.awscdk.services.route53.targets.CloudFrontTarget; +import software.amazon.awscdk.services.s3.Bucket; import software.amazon.awscdk.services.s3.deployment.BucketDeployment; import software.amazon.awscdk.services.s3.deployment.ISource; import software.amazon.awscdk.services.s3.deployment.Source; -import software.amazon.awscdk.services.certificatemanager.DnsValidatedCertificate; - public class StaticSiteStack extends Construct { - /** - * Static site infrastructure, which deploys site content to an S3 bucket. - * - * The site redirects from HTTP to HTTPS, using a CloudFront distribution, - * Route53 alias record, and ACM certificate. - */ - - public StaticSiteStack(Construct scope, String id, Map props) { - super(scope, id); - - try { - - final IHostedZone zone = HostedZone.fromLookup(this, "Zone", HostedZoneProviderProps.builder().domainName(props.get("domainName").toString()).build()); - - final String siteDomain = props.get("siteSubDomain").toString() + '.' - + props.get("domainName").toString(); - List siteDomainList = new ArrayList<>(1); - siteDomainList.add(siteDomain); - - // Site URL CfnOutput variable - CfnOutput.Builder.create(this, "Site") - .description("Site Domain Url") - .value("https://" + siteDomain).build(); - - // S3 Bucket resource and Content - Bucket siteBucket = Bucket.Builder.create(this, "SiteBucket") - .bucketName(siteDomain) - .websiteIndexDocument("index.html") - .websiteErrorDocument("error.html") - .publicReadAccess(false) - // The default removal policy is RETAIN, which means that cdk destroy will not attempt to delete - // the new bucket, and it will remain in your account until manually deleted. By setting the policy to - // DESTROY, cdk destroy will attempt to delete the bucket, but will error if the bucket is not empty. - .removalPolicy(RemovalPolicy.DESTROY).build(); - - - CfnOutput.Builder.create(this, "Bucket") - .description("Bucket Name") - .value(siteBucket.getBucketName()).build(); - - // TLS certificate - final String certificateArn = DnsValidatedCertificate.Builder.create(this, "SiteCertificate") - .domainName(siteDomain) - .hostedZone(zone).build().getCertificateArn(); - - CfnOutput.Builder.create(this, "Certificate") - .description("Certificate ARN") - .value(certificateArn).build(); - - // CloudFront distribution that provides HTTPS - - List behavioursList = new ArrayList<>(1); - behavioursList.add(Behavior.builder().isDefaultBehavior(true).build()); - - List sourceConfigurationsList = new ArrayList<>(1); - sourceConfigurationsList.add(SourceConfiguration.builder() - .s3OriginSource(S3OriginConfig.builder().s3BucketSource(siteBucket).build()) - .behaviors(behavioursList) - .build()); - - @SuppressWarnings({ "deprecation"}) - CloudFrontWebDistribution distribution = CloudFrontWebDistribution.Builder.create(this, "SiteDistribution") - .aliasConfiguration(AliasConfiguration.builder() - .acmCertRef(certificateArn) - .sslMethod(SSLMethod.SNI) - .securityPolicy(SecurityPolicyProtocol.TLS_V1_1_2016) - .names(siteDomainList) - .build()) - .originConfigs(sourceConfigurationsList) - .build(); - - CfnOutput.Builder.create(this, "DistributionId") - .description("CloudFront Distribution Id") - .value(distribution.getDistributionId()).build(); - - - // Route53 alias record for the CloudFront distribution - - ARecord.Builder.create(this, "SiteAliasRecord") - .recordName(siteDomain) - .target(RecordTarget.fromAlias(new CloudFrontTarget(distribution))) - .zone(zone) - .build(); - - // Deploy site contents to S3 bucket - List sources = new ArrayList<>(1); - sources.add(Source.asset("./site-contents")); - - BucketDeployment.Builder.create(this, "DeployWithInvalidation") - .sources(sources) - .destinationBucket(siteBucket) - .distribution(distribution) - .build(); - - - - } catch (Exception e) { - e.printStackTrace(); - } + /** + * Static site infrastructure, which deploys site content to an S3 bucket. + * + *

The site redirects from HTTP to HTTPS, using a CloudFront distribution, Route53 alias + * record, and ACM certificate. + */ + public StaticSiteStack(Construct scope, String id, Map props) { + super(scope, id); + + try { + + final IHostedZone zone = + HostedZone.fromLookup( + this, + "Zone", + HostedZoneProviderProps.builder() + .domainName(props.get("domainName").toString()) + .build()); + + final String siteDomain = + props.get("siteSubDomain").toString() + '.' + props.get("domainName").toString(); + List siteDomainList = new ArrayList<>(1); + siteDomainList.add(siteDomain); + + // Site URL CfnOutput variable + CfnOutput.Builder.create(this, "Site") + .description("Site Domain Url") + .value("https://" + siteDomain) + .build(); + + // S3 Bucket resource and Content + Bucket siteBucket = + Bucket.Builder.create(this, "SiteBucket") + .bucketName(siteDomain) + .websiteIndexDocument("index.html") + .websiteErrorDocument("error.html") + .publicReadAccess(false) + // The default removal policy is RETAIN, which means that cdk destroy will not attempt + // to delete + // the new bucket, and it will remain in your account until manually deleted. By + // setting the policy to + // DESTROY, cdk destroy will attempt to delete the bucket, but will error if the + // bucket is not empty. + .removalPolicy(RemovalPolicy.DESTROY) + .build(); + + CfnOutput.Builder.create(this, "Bucket") + .description("Bucket Name") + .value(siteBucket.getBucketName()) + .build(); + + // TLS certificate + final String certificateArn = + DnsValidatedCertificate.Builder.create(this, "SiteCertificate") + .domainName(siteDomain) + .hostedZone(zone) + .build() + .getCertificateArn(); + + CfnOutput.Builder.create(this, "Certificate") + .description("Certificate ARN") + .value(certificateArn) + .build(); + + // CloudFront distribution that provides HTTPS + + List behavioursList = new ArrayList<>(1); + behavioursList.add(Behavior.builder().isDefaultBehavior(true).build()); + + List sourceConfigurationsList = new ArrayList<>(1); + sourceConfigurationsList.add( + SourceConfiguration.builder() + .s3OriginSource(S3OriginConfig.builder().s3BucketSource(siteBucket).build()) + .behaviors(behavioursList) + .build()); + + @SuppressWarnings({"deprecation"}) + CloudFrontWebDistribution distribution = + CloudFrontWebDistribution.Builder.create(this, "SiteDistribution") + .aliasConfiguration( + AliasConfiguration.builder() + .acmCertRef(certificateArn) + .sslMethod(SSLMethod.SNI) + .securityPolicy(SecurityPolicyProtocol.TLS_V1_1_2016) + .names(siteDomainList) + .build()) + .originConfigs(sourceConfigurationsList) + .build(); + + CfnOutput.Builder.create(this, "DistributionId") + .description("CloudFront Distribution Id") + .value(distribution.getDistributionId()) + .build(); + + // Route53 alias record for the CloudFront distribution + + ARecord.Builder.create(this, "SiteAliasRecord") + .recordName(siteDomain) + .target(RecordTarget.fromAlias(new CloudFrontTarget(distribution))) + .zone(zone) + .build(); + + // Deploy site contents to S3 bucket + List sources = new ArrayList<>(1); + sources.add(Source.asset("./site-contents")); + + BucketDeployment.Builder.create(this, "DeployWithInvalidation") + .sources(sources) + .destinationBucket(siteBucket) + .distribution(distribution) + .build(); + } catch (Exception e) { + e.printStackTrace(); } + } } diff --git a/java/stepfunctions-job-poller/src/main/java/software/amazon/awscdk/examples/StepFunctionsJobPollerApp.java b/java/stepfunctions-job-poller/src/main/java/software/amazon/awscdk/examples/StepFunctionsJobPollerApp.java index 0d432227..6fa3afdb 100644 --- a/java/stepfunctions-job-poller/src/main/java/software/amazon/awscdk/examples/StepFunctionsJobPollerApp.java +++ b/java/stepfunctions-job-poller/src/main/java/software/amazon/awscdk/examples/StepFunctionsJobPollerApp.java @@ -3,11 +3,11 @@ import software.amazon.awscdk.core.App; public class StepFunctionsJobPollerApp { - public static void main(final String args[]) { - App app = new App(); - - new StepFunctionsJobPollerStack(app, "cdk-stepfunctions-jobpoller-example"); + public static void main(final String args[]) { + App app = new App(); - app.synth(); - } + new StepFunctionsJobPollerStack(app, "cdk-stepfunctions-jobpoller-example"); + + app.synth(); + } } diff --git a/java/stepfunctions-job-poller/src/main/java/software/amazon/awscdk/examples/StepFunctionsJobPollerStack.java b/java/stepfunctions-job-poller/src/main/java/software/amazon/awscdk/examples/StepFunctionsJobPollerStack.java index 01faff56..48e30e37 100644 --- a/java/stepfunctions-job-poller/src/main/java/software/amazon/awscdk/examples/StepFunctionsJobPollerStack.java +++ b/java/stepfunctions-job-poller/src/main/java/software/amazon/awscdk/examples/StepFunctionsJobPollerStack.java @@ -1,6 +1,5 @@ package software.amazon.awscdk.examples; - import software.amazon.awscdk.core.Construct; import software.amazon.awscdk.core.Duration; import software.amazon.awscdk.core.Stack; @@ -10,59 +9,69 @@ import software.amazon.awscdk.services.stepfunctions.Condition; import software.amazon.awscdk.services.stepfunctions.Fail; import software.amazon.awscdk.services.stepfunctions.StateMachine; -import software.amazon.awscdk.services.stepfunctions.tasks.InvokeActivity; import software.amazon.awscdk.services.stepfunctions.Task; import software.amazon.awscdk.services.stepfunctions.Wait; import software.amazon.awscdk.services.stepfunctions.WaitTime; +import software.amazon.awscdk.services.stepfunctions.tasks.InvokeActivity; public class StepFunctionsJobPollerStack extends Stack { - public StepFunctionsJobPollerStack(final Construct scope, final String id){ + public StepFunctionsJobPollerStack(final Construct scope, final String id) { super(scope, id); try { - Activity submitJobActivity = Activity.Builder.create(this, "SubmitJob").build(); - Activity checkJobActivity = Activity.Builder.create(this, "CheckJob").build(); - - Task submitJob = Task.Builder.create(this, "Submit Job") - .task(InvokeActivity.Builder.create(submitJobActivity).build()) - .resultPath("$.guid").build(); - - Wait waitX = Wait.Builder.create(this, "Wait X Seconds") - .time(WaitTime.secondsPath("$.wait_time")).build(); - - Task getStatus = Task.Builder.create(this, "Get Job Status") - .task(InvokeActivity.Builder.create(checkJobActivity).build()) - .inputPath("$.guid") - .resultPath("$.status").build(); - - Choice isComplete = Choice.Builder.create(this, "Job Complete?").build(); - Fail jobFailed = Fail.Builder.create(this, "Job Failed") - .cause("AWS Batch Job Failed") - .error("DescribeJob returned FAILED").build(); - - Task finalStatus = Task.Builder.create(this, "Get Final Job Status") - .task(InvokeActivity.Builder.create(checkJobActivity).build()) - .inputPath("$.guid").build(); - - Chain chain = Chain - .start(submitJob) - .next(waitX) - .next(getStatus) - .next(isComplete - .when(Condition.stringEquals("$.status", "FAILED"), jobFailed) - .when(Condition.stringEquals("$.status", "SUCCEEDED"), finalStatus) - .otherwise(waitX)); - - StateMachine.Builder.create(this, "StateMachine") - .definition(chain) - .timeout(Duration.seconds(30)).build(); - - + Activity submitJobActivity = Activity.Builder.create(this, "SubmitJob").build(); + Activity checkJobActivity = Activity.Builder.create(this, "CheckJob").build(); + + Task submitJob = + Task.Builder.create(this, "Submit Job") + .task(InvokeActivity.Builder.create(submitJobActivity).build()) + .resultPath("$.guid") + .build(); + + Wait waitX = + Wait.Builder.create(this, "Wait X Seconds") + .time(WaitTime.secondsPath("$.wait_time")) + .build(); + + Task getStatus = + Task.Builder.create(this, "Get Job Status") + .task(InvokeActivity.Builder.create(checkJobActivity).build()) + .inputPath("$.guid") + .resultPath("$.status") + .build(); + + Choice isComplete = Choice.Builder.create(this, "Job Complete?").build(); + Fail jobFailed = + Fail.Builder.create(this, "Job Failed") + .cause("AWS Batch Job Failed") + .error("DescribeJob returned FAILED") + .build(); + + Task finalStatus = + Task.Builder.create(this, "Get Final Job Status") + .task(InvokeActivity.Builder.create(checkJobActivity).build()) + .inputPath("$.guid") + .build(); + + Chain chain = + Chain.start(submitJob) + .next(waitX) + .next(getStatus) + .next( + isComplete + .when(Condition.stringEquals("$.status", "FAILED"), jobFailed) + .when(Condition.stringEquals("$.status", "SUCCEEDED"), finalStatus) + .otherwise(waitX)); + + StateMachine.Builder.create(this, "StateMachine") + .definition(chain) + .timeout(Duration.seconds(30)) + .build(); + } catch (Exception e) { e.printStackTrace(); } } - -} \ No newline at end of file +}