diff --git a/.env b/.env index 0c26008..f2a625c 100644 --- a/.env +++ b/.env @@ -1 +1 @@ -VERSION="1.4" \ No newline at end of file +VERSION="1.5" \ No newline at end of file diff --git a/README.md b/README.md index 14f6d1a..46ec751 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,10 @@ This is a serverless AWS Cloud hosted app that utilizes a CI/CD DevOps inspired * AWS Lambda * AWS Cloud Formation +* API Endpoint * GitHub Actions (YAML .yml) * GitHub hook `pre-commit` (BASH script) * Git // GitHub -* VS Code \ No newline at end of file +* VS Code + +## Thank you Pixegami for your guidance \ No newline at end of file diff --git a/lambda/main.py b/lambda/main.py index fd02a2c..ca77f0f 100644 --- a/lambda/main.py +++ b/lambda/main.py @@ -1,9 +1,32 @@ import os +import boto3 def handler(event, context): + # Raw event data; kill the favicon hit + path = event["rawPath"] + if path != "/": + return {"statusCode": 404, "body": "Not found."} + + # get a reference to dynamoDB + dynamodb = boto3.resource("dynamodb") + table = dynamodb.Table(os.environ.get("TABLE_NAME")) + + # Read the "VISIT COUNT" key (or create it if it doesn't exist) + response = table.get_item(Key={"key": "visit_count"}) + if "Item" in response: + visit_count = response["Item"]["value"] + else: + visit_count = 0 + + # Increment the visit count and write it back to the table. + new_visit_count = visit_count + 1 + table.put_item(Item={"key": "visit_count", "value": new_visit_count}) + + # get current version from .env version = os.environ.get("VERSION", "0.0") response_body = { "message": "Hello World", "version": version, + "visit_count": new_visit_count, } return {"statusCode": 200, "body": response_body} \ No newline at end of file diff --git a/lib/aws-github-actions-cicd-stack.ts b/lib/aws-github-actions-cicd-stack.ts index 7ffdc22..80b8d75 100644 --- a/lib/aws-github-actions-cicd-stack.ts +++ b/lib/aws-github-actions-cicd-stack.ts @@ -1,6 +1,7 @@ import * as cdk from 'aws-cdk-lib'; import { Construct } from 'constructs'; import * as lambda from 'aws-cdk-lib/aws-lambda' +import * as dynamodb from "aws-cdk-lib/aws-dynamodb"; import * as dotenv from "dotenv"; export class AwsGithubActionsCicdStack extends cdk.Stack { @@ -10,16 +11,28 @@ export class AwsGithubActionsCicdStack extends cdk.Stack { // Load the environment .env file. dotenv.config(); - // create a lambda function + // Create a table to store some data. + const table = new dynamodb.Table(this, "VisitorTimeTable", { + partitionKey: { + name: "key", + type: dynamodb.AttributeType.STRING, + }, + billingMode: dynamodb.BillingMode.PAY_PER_REQUEST, + }); + + // create a lambda function with access to dynamodb const lambdaFunction = new lambda.Function(this, "LambdaFunction", { runtime: lambda.Runtime.PYTHON_3_9, code: lambda.Code.fromAsset("lambda"), handler: "main.handler", environment: { - VERSION: process.env.VERSION || "0.0" + VERSION: process.env.VERSION || "0.0", + TABLE_NAME: table.tableName, }, }); + table.grantReadWriteData(lambdaFunction) + // create url const functionUrl = lambdaFunction.addFunctionUrl({ authType: lambda.FunctionUrlAuthType.NONE,