Skip to content

Commit

Permalink
Merge pull request aws-samples#1 from MrArnoldPalmer/pr/188
Browse files Browse the repository at this point in the history
Example Formatting
  • Loading branch information
zechariahks authored Dec 5, 2019
2 parents fc47974 + e5b1037 commit 70765ea
Show file tree
Hide file tree
Showing 6 changed files with 273 additions and 256 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
Original file line number Diff line number Diff line change
@@ -1,69 +1,67 @@
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;
import software.amazon.awscdk.core.Stack;
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<String, Object> map = new HashMap<String, Object>();
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<String, Object> map = new HashMap<String, Object>();
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;
}
}
Original file line number Diff line number Diff line change
@@ -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<String, Object> domainValues = new HashMap<String, Object>();
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<String, Object> domainValues = new HashMap<String, Object>();
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();
}
}


Loading

0 comments on commit 70765ea

Please sign in to comment.