Skip to content

Commit

Permalink
Refactor CDK example deploy
Browse files Browse the repository at this point in the history
  • Loading branch information
cwygoda committed Apr 18, 2024
1 parent fb82c2e commit 57d44cf
Show file tree
Hide file tree
Showing 10 changed files with 295 additions and 53 deletions.
28 changes: 28 additions & 0 deletions .github/actions/cdk-deploy/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
name: run cdk deploy
description: deploy example to AWS using CDK
inputs:
AWS_SECRET_ACCESS_KEY:
required: true
AWS_ACCESS_KEY_ID:
required: true
AWS_ECR_REPOSITORY_ARN:
required: true
IMAGE_TAG_OR_DIGEST:
required: true

runs:
using: "composite"
steps:
- uses: actions/setup-node@v4
with:
node-version: 18.x
- run: npm install -g aws-cdk
shell: bash
- run: poetry run cdk deploy --require-approval=never
shell: bash
working-directory: deployment
env:
AWS_SECRET_ACCESS_KEY: ${{ inputs.AWS_SECRET_ACCESS_KEY }}
AWS_ACCESS_KEY_ID: ${{ inputs.AWS_ACCESS_KEY_ID }}
AWS_ECR_REPOSITORY_ARN: ${{ inputs.AWS_ECR_REPOSITORY_ARN }}
IMAGE_TAG_OR_DIGEST: ${{ inputs.IMAGE_TAG_OR_DIGEST }}
28 changes: 28 additions & 0 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
name: Deploy demo to AWS

on:
workflow_dispatch:
inputs:
image_tag_or_digest:
description: docker image tag or digest to deploy
required: true
default: latest
type: string

jobs:
deploy:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- run: pipx install poetry==1.7.1
- uses: actions/setup-python@v5
with:
python-version: "3.12"
cache: poetry
- name: Run CDK deploy
uses: ./.github/actions/cdk-deploy
with:
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
AWS_ACCESS_KEY_ID: ${{ vars.AWS_ACCESS_KEY_ID }}
AWS_ECR_REPOSITORY_ARN: ${{ vars.AWS_ECR_REPOSITORY_ARN }}
IMAGE_TAG_OR_DIGEST: ${{ inputs.image_tag_or_digest }}
1 change: 1 addition & 0 deletions deployment/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
cdk.out
Empty file added deployment/__init__.py
Empty file.
54 changes: 27 additions & 27 deletions deployment/app.py
Original file line number Diff line number Diff line change
@@ -1,58 +1,58 @@
import os
from typing import Any, Dict, Optional

import aws_cdk.aws_apigatewayv2_alpha as apigatewayv2
import aws_cdk.aws_apigatewayv2_integrations_alpha as apigateway_integrations
import aws_cdk.aws_apigatewayv2 as apigatewayv2
import aws_cdk.aws_apigatewayv2_integrations as apigateway_integrations
import aws_cdk.aws_lambda as _lambda
from aws_cdk import App, CfnOutput, Duration, Stack
from aws_cdk import aws_logs as logs
from aws_cdk.aws_ecr import Repository
from constructs import Construct

from deployment.profile import DeploymentProfile


class STATDeploy(Stack):
def __init__(
self,
scope: Construct,
id: str,
memory: int = 1024,
timeout: int = 30,
runtime: _lambda.Runtime = _lambda.Runtime.PYTHON_3_11, # Set to 3.11 as the aws_cdk version does not have 3.12
concurrent: Optional[int] = None,
environment: Optional[Dict] = None,
code_dir: str = "/",
**kwargs: Any,
profile: DeploymentProfile,
) -> None:
super().__init__(scope, id, **kwargs)
super().__init__(scope, id)

repository = Repository.from_repository_arn(
self, "ImageRepository", profile.aws_ecr_repository_arn
)

lambda_function = _lambda.Function(
self,
f"{id}-lambda",
runtime=runtime,
code=_lambda.Code.from_docker_build(
path=os.path.abspath(code_dir),
file="./Dockerfile",
"ServiceLambda",
runtime=_lambda.Runtime.FROM_IMAGE,
handler=_lambda.Handler.FROM_IMAGE,
code=_lambda.Code.from_ecr_image(
repository=repository,
tag_or_digest=profile.image_tag_or_digest,
entrypoint=["python3"],
cmd=["/app/lambda_handler.py"],
),
handler="handler.handler",
memory_size=memory,
reserved_concurrent_executions=concurrent,
timeout=Duration.seconds(timeout),
environment=environment,
log_retention=logs.RetentionDays.ONE_WEEK, # Honestly can be removed not sure if required at this stage
memory_size=profile.memory,
timeout=Duration.seconds(profile.timeout),
environment={},
log_retention=logs.RetentionDays.ONE_WEEK,
)

api = apigatewayv2.HttpApi(
self,
f"{id}-endpoint",
"ServiceGateway",
default_integration=apigateway_integrations.HttpLambdaIntegration(
f"{id}-integration", handler=lambda_function
"LambdaIntegration", handler=lambda_function
),
)
CfnOutput(self, "Endpoint", value=api.url)


def main():
app = App()
STATDeploy(app, "MyCdkProjectStack")
profile = DeploymentProfile()
STATDeploy(app, "StatExampleStack", profile)
app.synth()


Expand Down
14 changes: 0 additions & 14 deletions deployment/cdk.json
Original file line number Diff line number Diff line change
@@ -1,19 +1,5 @@
{
"app": "python3 app.py",
"watch": {
"include": [
"**"
],
"exclude": [
"README.md",
"cdk*.json",
"requirements*.txt",
"source.bat",
"**/__init__.py",
"**/__pycache__",
"tests"
]
},
"context": {
"@aws-cdk/aws-lambda:recognizeLayerVersion": true,
"@aws-cdk/core:checkSecretUsage": true,
Expand Down
4 changes: 0 additions & 4 deletions deployment/handler.py

This file was deleted.

9 changes: 9 additions & 0 deletions deployment/profile.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from pydantic import PositiveInt
from pydantic_settings import BaseSettings


class DeploymentProfile(BaseSettings):
memory: PositiveInt = 1024
timeout: PositiveInt = 30
aws_ecr_repository_arn: str
image_tag_or_digest: str = "latest"
Loading

0 comments on commit 57d44cf

Please sign in to comment.