forked from aws-samples/aws-glue-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request aws-samples#1 from MrArnoldPalmer/pr/188
Example Formatting
- Loading branch information
Showing
6 changed files
with
273 additions
and
256 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
104 changes: 51 additions & 53 deletions
104
java/custom-resource/src/main/java/software/amazon/awscdk/examples/CustomResourceStack.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
80 changes: 38 additions & 42 deletions
80
java/static-site/src/main/java/software/amazon/awscdk/examples/StaticSiteApp.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
|
||
|
Oops, something went wrong.