-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature: add serverless template for closed network system workloads
Add CDK code to build an environment to run serverless sample applications. If the number of accesses is low or there is a time period where there is almost no access, running servers all the time using ECS/Fargate costs a lot compared to the actual usage amount. There are also operating costs for container images. In such cases, reduce costs and operational troubles by using serverless by S3 and Lambda. Hosting an internal HTTPS static website using ALB, S3, and PrivateLink in a closed network is described in this blog. --------- Signed-off-by: dependabot[bot] <[email protected]> Co-authored-by: Amazon GitHub Automation <[email protected]> Co-authored-by: Yozo Suzuki <[email protected]> Co-authored-by: suzukyz <[email protected]> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: Ray Oshikawa <[email protected]>
- Loading branch information
1 parent
0b6d0a3
commit bfbaf9c
Showing
80 changed files
with
27,780 additions
and
111 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
*/.DS_Store | ||
.DS_Store |
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
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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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 |
---|---|---|
@@ -0,0 +1 @@ | ||
node_modules |
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 |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import Connection from "./lib/connect"; | ||
import { Logger } from "@aws-lambda-powertools/logger"; | ||
import { APIGatewayProxyEvent, APIGatewayProxyResult } from "aws-lambda"; | ||
|
||
const logger = new Logger({ serviceName: "getLambda" }); | ||
|
||
export const handler = async ( | ||
event: APIGatewayProxyEvent | ||
): Promise<APIGatewayProxyResult> => { | ||
try { | ||
const client = await Connection(); | ||
// Connection | ||
await client.connect(); | ||
logger.info("connected"); | ||
|
||
// Query | ||
const res = await client.query( | ||
"SELECT * FROM sampleapp_table WHERE id = 1" | ||
); | ||
const response = { | ||
statusCode: 200, | ||
body: | ||
res.rows.length > 0 ? JSON.stringify(res.rows[0]) : JSON.stringify(""), | ||
}; | ||
return response; | ||
} catch (e) { | ||
logger.error(e.toString()); | ||
const response = { | ||
statusCode: 500, | ||
body: JSON.stringify("Server error"), | ||
}; | ||
return response; | ||
} | ||
}; |
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 |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import Connection from "./lib/connect"; | ||
import { Logger } from "@aws-lambda-powertools/logger"; | ||
import cfnResponse from "cfn-response"; | ||
|
||
const logger = new Logger({ serviceName: "initLambda" }); | ||
|
||
export const handler = async (event: any, context: any): Promise<any> => { | ||
if (event.RequestType == "Create" || event.RequestType == "Update") { | ||
try { | ||
const client = await Connection(); | ||
// Connection | ||
await client.connect(); | ||
logger.info("connected"); | ||
|
||
// Query | ||
const res1 = await client.query("DROP TABLE IF EXISTS sampleapp_table;"); | ||
logger.info(res1); | ||
const res2 = await client.query( | ||
'CREATE TABLE IF NOT EXISTS sampleapp_table(id serial NOT NULL,name text COLLATE pg_catalog."default" NOT NULL,job0001_flag boolean NOT NULL DEFAULT false,job0002_flag boolean NOT NULL DEFAULT false,job0003_flag boolean NOT NULL DEFAULT false,job0004_flag boolean NOT NULL DEFAULT false,job0005_flag boolean NOT NULL DEFAULT false,CONSTRAINT sample_app_pkey PRIMARY KEY (id));' | ||
); | ||
logger.info(res2); | ||
const res3 = await client.query( | ||
"INSERT INTO sampleapp_table(name, job0001_flag, job0002_flag, job0003_flag, job0004_flag, job0005_flag) VALUES ('test record 1',true,true,true,true,true);" | ||
); | ||
logger.info(res3); | ||
return cfnResponse.send( | ||
event, | ||
context, | ||
cfnResponse.SUCCESS, | ||
{ message: Date.now().toString() }, | ||
event.PhysicalResourceId | ||
); | ||
} catch (e) { | ||
logger.error(e.toString()); | ||
return cfnResponse.send( | ||
event, | ||
context, | ||
cfnResponse.SUCCESS, | ||
{ message: Date.now().toString() }, | ||
event.PhysicalResourceId | ||
); | ||
} | ||
} | ||
return cfnResponse.send( | ||
event, | ||
context, | ||
cfnResponse.SUCCESS, | ||
{ message: Date.now().toString() }, | ||
event.PhysicalResourceId | ||
); | ||
}; |
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 |
---|---|---|
@@ -0,0 +1,36 @@ | ||
const referSecrets = async () => { | ||
const { SecretsManagerClient, GetSecretValueCommand } = await import( | ||
"@aws-sdk/client-secrets-manager" | ||
); | ||
const secretsManager = new SecretsManagerClient({ | ||
region: process.env.REGION!, | ||
}); | ||
const response = await secretsManager.send( | ||
new GetSecretValueCommand({ | ||
SecretId: process.env.SECRET_NAME!, | ||
}) | ||
); | ||
return JSON.parse(response.SecretString!); | ||
}; | ||
|
||
export default async function Connection() { | ||
const { Client } = await import("pg"); | ||
const secrets = await referSecrets(); | ||
const { Signer } = await import("@aws-sdk/rds-signer"); | ||
const signer = new Signer({ | ||
region: process.env.REGION!, | ||
username: secrets.username, | ||
hostname: process.env.HOST!, | ||
port: secrets.port, | ||
}); | ||
const token = await signer.getAuthToken(); | ||
// client settings | ||
const client = new Client({ | ||
host: process.env.HOST!, | ||
port: secrets.port, | ||
user: secrets.username, | ||
password: token, //secrets.password, | ||
ssl: true, | ||
}); | ||
return client; | ||
} |
Oops, something went wrong.