Skip to content

Commit

Permalink
update readme add visitor count via ddb/cdk/aws
Browse files Browse the repository at this point in the history
  • Loading branch information
TIMOTHY ANDES committed Mar 1, 2024
1 parent 0b04010 commit 8610548
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 4 deletions.
2 changes: 1 addition & 1 deletion .env
Original file line number Diff line number Diff line change
@@ -1 +1 @@
VERSION="1.4"
VERSION="1.5"
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
* VS Code

## Thank you Pixegami for your guidance
23 changes: 23 additions & 0 deletions lambda/main.py
Original file line number Diff line number Diff line change
@@ -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}
17 changes: 15 additions & 2 deletions lib/aws-github-actions-cicd-stack.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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,
Expand Down

0 comments on commit 8610548

Please sign in to comment.