Skip to content

Commit

Permalink
Align resource naming conventions
Browse files Browse the repository at this point in the history
  • Loading branch information
alexpulver committed Dec 7, 2021
1 parent f27f316 commit 0eb9440
Show file tree
Hide file tree
Showing 13 changed files with 172 additions and 278 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ __pycache__/
*.so

# CDK-Dia
diagram.png*
/diagram.*

# Coverage.py
.coverage
Expand Down
30 changes: 15 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
# User management backend
The project implements a *user management backend* component that uses
Amazon API Gateway, AWS Lambda and Amazon DynamoDB to provide basic
CRUD operations for managing users. The project includes a pipeline
with continuous deployment and pull request build.
CRUD operations for managing users. The project includes continuous
deployment and pull request validation.

![diagram](https://user-images.githubusercontent.com/4362270/132720458-35b39091-61ed-413e-a9d5-28967053d64d.png)
![diagram](https://user-images.githubusercontent.com/4362270/144895542-6b8f3777-c0c7-4cc7-9399-43f222e35a0c.png)
\* Diagram generated using https://github.com/pistazie/cdk-dia

## Create a new repository from usermanagement-backend
This is optional for deploying the development stage, but **required** for deploying
the pipeline.
This is optional for deploying the development stage, but **required** for
continuous deployment and pull request validation.

The instructions below use the usermanagement-backend repository.

Expand Down Expand Up @@ -69,26 +69,26 @@ npx cdk deploy "UserManagementBackend-Dev/*"

Example outputs for `npx cdk deploy "UserManagementBackend-Dev/*"`:
```text
✅ UserManagementBackendDevStatefulB4115ED0 (UserManagementBackend-Dev-Stateful) (no changes)
✅ UserManagementBackendDevStatefulB4115ED0 (UserManagementBackend-Dev-Stateful)
Outputs:
UserManagementBackendDevStatefulB4115ED0.ExportsOutputFnGetAttDatabaseTableF104A135ArnDAC15A6A = arn:aws:dynamodb:eu-west-1:807650736403:table/UserManagementBackend-Dev-Stateful-DatabaseTableF104A135-1JZ4KML3DEAMJ
UserManagementBackendDevStatefulB4115ED0.ExportsOutputRefDatabaseTableF104A1356B7D7D8A = UserManagementBackend-Dev-Stateful-DatabaseTableF104A135-1JZ4KML3DEAMJ
UserManagementBackendDevStatefulB4115ED0.ExportsOutputFnGetAttDatabaseDynamoDbTableB13A2BF6ArnA67170C6 = arn:aws:dynamodb:eu-west-1:807650736403:table/UserManagementBackend-Dev-Stateful-DatabaseDynamoDbTableB13A2BF6-AKTEOTYGW3A
UserManagementBackendDevStatefulB4115ED0.ExportsOutputRefDatabaseDynamoDbTableB13A2BF6AB01B0C4 = UserManagementBackend-Dev-Stateful-DatabaseDynamoDbTableB13A2BF6-AKTEOTYGW3A
```
```text
✅ UserManagementBackendDevStatelessAD73535F (UserManagementBackend-Dev-Stateless)
Outputs:
UserManagementBackendDevStatelessAD73535F.APIEndpointURL = https://ctixe0v786.execute-api.eu-west-1.amazonaws.com/
UserManagementBackendDevStatelessAD73535F.ApiEndpoint = https://ctixe0v786.execute-api.eu-west-1.amazonaws.com/
```

## Deploy the pipeline
## Deploy the toolchain

**Prerequisites**
- Create a new repository from usermanagement-backend, if you haven't done this already
- Create AWS CodeStar Connections [connection](https://docs.aws.amazon.com/dtconsole/latest/userguide/welcome-connections.html)
for the continuous deployment
- Authorize AWS CodeBuild access for the pull request build
for continuous deployment
- Authorize AWS CodeBuild access for the pull request validation
- Start creating a new project manually
- Select GitHub as Source provider
- Choose **Connect using OAuth**
Expand All @@ -97,14 +97,14 @@ UserManagementBackendDevStatelessAD73535F.APIEndpointURL = https://ctixe0v786.ex
- Commit and push the changes: `git commit -a -m 'Update constants' && git push`

```bash
npx cdk deploy UserManagementBackend-Pipeline
npx cdk deploy UserManagementBackend-Toolchain
```

## Delete all stacks
**Do not forget to delete the stacks to avoid unexpected charges**
```bash
npx cdk destroy "UserManagementBackend-Dev/*"
npx cdk destroy UserManagementBackend-Pipeline
npx cdk destroy UserManagementBackend-Toolchain
npx cdk destroy "UserManagementBackend-Prod/*"
```

Expand All @@ -117,7 +117,7 @@ Below are examples that show the available resources and how to use them.
```bash
endpoint_url=$(aws cloudformation describe-stacks \
--stack-name UserManagementBackend-Dev-Stateless \
--query 'Stacks[*].Outputs[?OutputKey==`APIEndpointURL`].OutputValue' \
--query 'Stacks[*].Outputs[?OutputKey==`ApiEndpoint`].OutputValue' \
--output text)

curl \
Expand Down
6 changes: 3 additions & 3 deletions api/infrastructure.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from aws_cdk import core as cdk


class API(cdk.Construct):
class Api(cdk.Construct):
def __init__(
self,
scope: cdk.Construct,
Expand All @@ -33,6 +33,6 @@ def __init__(
lambda_integration = apigatewayv2_integrations.LambdaProxyIntegration(
handler=self.lambda_function
)
self.http_api = apigatewayv2.HttpApi(
self, "HttpApi", default_integration=lambda_integration
self.api_gateway_http_api = apigatewayv2.HttpApi(
self, "ApiGatewayHttpApi", default_integration=lambda_integration
)
10 changes: 5 additions & 5 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import constants
from deployment import UserManagementBackend
from pipeline import Pipeline
from toolchain import Toolchain

app = cdk.App()

Expand All @@ -15,11 +15,11 @@
database_dynamodb_billing_mode=constants.DEV_DATABASE_DYNAMODB_BILLING_MODE,
)

# Continuous deployment and pull request build
Pipeline(
# Continuous deployment and pull request validation
Toolchain(
app,
f"{constants.CDK_APP_NAME}-Pipeline",
env=constants.PIPELINE_ENV,
f"{constants.CDK_APP_NAME}-Toolchain",
env=constants.TOOLCHAIN_ENV,
)

app.synth()
2 changes: 1 addition & 1 deletion constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
GITHUB_REPO = "usermanagement-backend"
GITHUB_TRUNK_BRANCH = "main"

PIPELINE_ENV = cdk.Environment(account="807650736403", region="eu-west-1")
TOOLCHAIN_ENV = cdk.Environment(account="807650736403", region="eu-west-1")

DEV_ENV = cdk.Environment(
account=os.environ["CDK_DEFAULT_ACCOUNT"], region=os.environ["CDK_DEFAULT_REGION"]
Expand Down
2 changes: 1 addition & 1 deletion database/infrastructure.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def __init__(
)
self.dynamodb_table = dynamodb.Table(
self,
"Table",
"DynamoDbTable",
billing_mode=dynamodb_billing_mode,
partition_key=partition_key,
removal_policy=cdk.RemovalPolicy.DESTROY,
Expand Down
12 changes: 6 additions & 6 deletions deployment.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from aws_cdk import aws_iam as iam
from aws_cdk import core as cdk

from api.infrastructure import API
from api.infrastructure import Api
from database.infrastructure import Database
from monitoring.infrastructure import Monitoring

Expand All @@ -26,19 +26,19 @@ def __init__(
stateful, "Database", dynamodb_billing_mode=database_dynamodb_billing_mode
)
stateless = cdk.Stack(self, "Stateless")
api = API(
api = Api(
stateless,
"API",
"Api",
database_dynamodb_table_name=database.dynamodb_table.table_name,
lambda_reserved_concurrency=api_lambda_reserved_concurrency,
)
database.dynamodb_table.grant_read_write_data(
cast(iam.IGrantable, api.lambda_function.role)
)
self.api_endpoint_url_cfn_output = cdk.CfnOutput(
self.api_endpoint = cdk.CfnOutput(
stateless,
"APIEndpointURL",
"ApiEndpoint",
# API doesn't disable create_default_stage, hence URL will be defined
value=api.http_api.url, # type: ignore
value=api.api_gateway_http_api.url, # type: ignore
)
Monitoring(stateless, "Monitoring", database=database, api=api)
10 changes: 6 additions & 4 deletions monitoring/infrastructure.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
from aws_cdk import aws_cloudwatch as cloudwatch
from aws_cdk import core as cdk

from api.infrastructure import API
from api.infrastructure import Api
from database.infrastructure import Database


class Monitoring(cdk.Construct):
def __init__(self, scope: cdk.Construct, id_: str, *, database: Database, api: API):
def __init__(self, scope: cdk.Construct, id_: str, *, database: Database, api: Api):
super().__init__(scope, id_)

widgets = [
cloudwatch.SingleValueWidget(metrics=[api.http_api.metric_count()]),
cloudwatch.SingleValueWidget(
metrics=[api.api_gateway_http_api.metric_count()]
),
cloudwatch.SingleValueWidget(
metrics=[database.dynamodb_table.metric_consumed_read_capacity_units()]
),
]
cloudwatch.Dashboard(self, "Dashboard", widgets=[widgets])
cloudwatch.Dashboard(self, "CloudWatchDashboard", widgets=[widgets])
95 changes: 58 additions & 37 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 0eb9440

Please sign in to comment.