merge files from the blockchain infra repo:

This commit is contained in:
bt3gl 2024-11-17 16:59:51 -08:00
parent 23f56ef195
commit 5a9703cfd5
346 changed files with 29097 additions and 132 deletions

View file

@ -0,0 +1,70 @@
# Setting up a PostgreSQL RDS with CDK in Python
### Create a virtual environment and install dependencies:
```
virtualenv .env
source .env/bin/activate
pip3 install -r requirements.txt
```
### Define You RDS DB
Add any constant variable in `cdk.json` and then define how you want your RDS instance in `postgre_sql_example/postgre_sql_example_stack.py`:
```
class PostgreSqlExampleStack(core.Stack):
def __init__(self, scope: core.Construct, id: str, **kwargs) -> None:
super().__init__(scope, id, **kwargs)
# Database Instance
instance = rds.DatabaseInstance(self,
'examplepostgresdbinstance',
master_username=master_username,
engine=rds.DatabaseInstanceEngine.POSTGRES, instance_class=ec2.InstanceType.of(ec2.InstanceClass.BURSTABLE2, ec2.InstanceSize.MICRO),
vpc=self.vpc,
auto_minor_version_upgrade=auto_minor_version_upgrade,
availability_zone=availability_zone,
database_name=database_name,
enable_performance_insights=enable_performance_insights,
storage_encrypted=storage_encrypted,
multi_az=multi_az,
backup_retention=backup_retention,
monitoring_interval=monitoring_interval,
)
```
### Create synthesized CloudFormation templates
```
cdk synth
```
You can check what changes are introduced into your current AWS resources with:
```
cdk diff --profile <AWS PROFILE>
```
### Deploy to AWS
If everything looks OK, deploy with:
```
cdk deploy --profile <AWS PROFILE>
```
To check all the stacks in the app:
```
cdk ls
```
### Clean up
To destroy/remove all the newly created resources, run:
```
cdk destroy --profile <AWS PROFILE>
```

View file

@ -0,0 +1,11 @@
#!/usr/bin/env python3
from aws_cdk import core
from postgre_sql_example.postgre_sql_example_stack import PostgreSqlExampleStack
app = core.App()
PostgreSqlExampleStack(app, "postgre-sql-example")
app.synth()

View file

@ -0,0 +1,16 @@
{
"app": "python3 app.py",
"context": {
"rds.auto_minor_version_upgrade": false,
"rds.availability_zone":
"rds.backup_retention":
"rds.database_name": "postgres_db",
"rds.enable_performance_insights": true,
"rds.master_username": "postgres",
"rds.monitoring_interval": 60,
"rds.multi_az": false,
"rds.storage_encrypted": false,
"vpc.cidr": "10.0.0.0/16",
"vpc.max_azs":
}
}

View file

@ -0,0 +1,59 @@
import json
import sys
from aws_cdk import (
aws_ec2 as ec2,
aws_rds as rds,
core as core,
)
# Python CDK does not have get_context yet.
def _get_context():
CONTEXT_FILE = 'cdk.json'
try:
with open(CONTEXT_FILE, 'r') as f:
return json.load(f)['context']
except IOError:
print('Could not open context file {}. Exiting...'.format(CONTEXT_FILE))
sys.exit(1)
except KeyError as e:
print('Context file {0} is misconfigured {1}. Exiting...'.format(CONTEXT_FILE, e))
sys.exit(1)
class PostgreSqlExampleStack(core.Stack):
def __init__(self, scope: core.Construct, id: str, **kwargs) -> None:
super().__init__(scope, id, **kwargs)
# Grab variables from cdk.json
context = _get_context()
auto_minor_version_upgrade = context["rds.auto_minor_version_upgrade"]
availability_zone = context["rds.availability_zone"]
backup_retention = core.Duration.days(context["rds.backup_retention"])
database_name = context["rds.database_name"]
enable_performance_insights = context["rds.enable_performance_insights"]
master_username = context["rds.master_username"]
monitoring_interval = core.Duration.seconds(context["rds.monitoring_interval"])
multi_az = context["rds.multi_az"]
storage_encrypted = context["rds.storage_encrypted"]
cidr = context["vpc.cidr"]
max_azs = context["vpc.max_azs"]
# Set VPC
self.vpc = ec2.Vpc(self, "VPCTest", cidr=cidr, max_azs=max_azs)
# Database Instance
instance = rds.DatabaseInstance(self,
'storefrontrdspostgresdbinstance',
master_username=master_username,
engine=rds.DatabaseInstanceEngine.POSTGRES, instance_class=ec2.InstanceType.of(ec2.InstanceClass.BURSTABLE2, ec2.InstanceSize.MICRO),
vpc=self.vpc,
auto_minor_version_upgrade=auto_minor_version_upgrade,
availability_zone=availability_zone,
database_name=database_name,
enable_performance_insights=enable_performance_insights,
storage_encrypted=storage_encrypted,
multi_az=multi_az,
backup_retention=backup_retention,
monitoring_interval=monitoring_interval,
)

View file

@ -0,0 +1,34 @@
astroid==2.2.5
attrs==19.1.0
aws-cdk.assets==1.10.0
aws-cdk.aws-cloudwatch==1.10.0
aws-cdk.aws-ec2==1.10.0
aws-cdk.aws-events==1.10.0
aws-cdk.aws-iam==1.10.0
aws-cdk.aws-kms==1.10.0
aws-cdk.aws-lambda==1.10.0
aws-cdk.aws-logs==1.10.0
aws-cdk.aws-rds==1.10.0
aws-cdk.aws-s3==1.10.0
aws-cdk.aws-s3-assets==1.10.0
aws-cdk.aws-sam==1.10.0
aws-cdk.aws-secretsmanager==1.10.0
aws-cdk.aws-sqs==1.10.0
aws-cdk.aws-ssm==1.10.0
aws-cdk.core==1.10.0
aws-cdk.cx-api==1.10.0
aws-cdk.region-info==1.10.0
cattrs==0.9.0
isort==4.3.21
jsii==0.17.1
lazy-object-proxy==1.4.2
mccabe==0.6.1
pep8==1.7.1
publication==0.0.3
pylint==2.3.1
python-dateutil==2.8.0
six==1.12.0
typed-ast==1.4.0
typing-extensions==3.7.4
virtualenv==16.7.4
wrapt==1.11.2

View file

@ -0,0 +1,45 @@
import setuptools
with open("README.md") as fp:
long_description = fp.read()
setuptools.setup(
name="postgre_sql_example",
version="0.0.1",
description="A postgres CDK Python example",
long_description=long_description,
long_description_content_type="text/markdown",
author="author",
package_dir={"": "postgre_sql_example"},
packages=setuptools.find_packages(where="postgre_sql_example"),
install_requires=[
"aws-cdk.core",
],
python_requires=">=3.6",
classifiers=[
"Development Status :: 4 - Beta",
"Intended Audience :: Developers",
"License :: OSI Approved :: Apache Software License",
"Programming Language :: JavaScript",
"Programming Language :: Python :: 3 :: Only",
"Programming Language :: Python :: 3.6",
"Programming Language :: Python :: 3.7",
"Programming Language :: Python :: 3.8",
"Topic :: Software Development :: Code Generators",
"Topic :: Utilities",
"Typing :: Typed",
],
)

View file

@ -0,0 +1,69 @@
# Setting up a VPC with CDK in Python
[AWS CDK](https://docs.aws.amazon.com/cdk/latest/guide/home.html) is a very neat way to write infrastructure as code, enabling you to create and provision AWS infrastructure deployments predictably and repeatedly.
You choose your favorite language to code what resources (stacks) you want, and CDK synthetizes them to CloudFormation and helps you to deploy them to AWS.
In this example we see how to setup a VPC using CDK in Python.
### Install AWS CDK
Follow [this instructions](https://github.com/aws/aws-cdk#at-a-glance).
### Create a virtual environment and install dependencies:
```
virtualenv venv
source venv/bin/activate
pip install -r requirements.txt
```
Note: If you are starting from a blank project with `cdk init app --language=python` instead, you will we need to manually install resources, such as `pip install aws_cdk.aws_ec2`.
### Define You VPC
Define how you want your VPC in the file `vpc_example/vpc_example_stack.py`:
```
class VpcExampleStack(core.Stack):
def __init__(self, scope: core.Construct, id: str, **kwargs) -> None:
super().__init__(scope, id, **kwargs)
vpc = aws_ec2.Vpc(self, "MiaVPCTest", cidr="10.0.0.0/16", max_azs=3)
```
### Create synthesized CloudFormation template
```
cdk synth
```
You can check what changes this introduces into your AWS account:
```
cdk diff --profile <AWS PROFILE>
```
### Deploy to AWS
If everything looks right, deploy:
```
cdk deploy --profile <AWS PROFILE>
```
To check all the stacks in the app:
```
cdk ls
```
### Clean up
To destroy/remove all the newly created resources, run:
```
cdk destroy --profile <AWS PROFILE>
```

View file

@ -0,0 +1,11 @@
#!/usr/bin/env python3
from aws_cdk import core
from vpc_example.vpc_example_stack import VpcExampleStack
app = core.App()
VpcExampleStack(app, "vpc-example")
app.synth()

View file

@ -0,0 +1,3 @@
{
"app": "python3 app.py"
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 771 KiB

View file

@ -0,0 +1,14 @@
attrs==19.1.0
aws-cdk.aws-cloudwatch==1.10.0
aws-cdk.aws-ec2==1.10.0
aws-cdk.aws-iam==1.10.0
aws-cdk.aws-ssm==1.10.0
aws-cdk.core==1.10.0
aws-cdk.cx-api==1.10.0
aws-cdk.region-info==1.10.0
cattrs==0.9.0
jsii==0.17.1
publication==0.0.3
python-dateutil==2.8.0
six==1.12.0
typing-extensions==3.7.4

View file

@ -0,0 +1,45 @@
import setuptools
with open("README.md") as fp:
long_description = fp.read()
setuptools.setup(
name="vpc_example",
version="0.0.1",
description="A VPC CDK Python example",
long_description=long_description,
long_description_content_type="text/markdown",
author="author",
package_dir={"": "vpc_example"},
packages=setuptools.find_packages(where="vpc_example"),
install_requires=[
"aws-cdk.core",
],
python_requires=">=3.6",
classifiers=[
"Development Status :: 4 - Beta",
"Intended Audience :: Developers",
"License :: OSI Approved :: Apache Software License",
"Programming Language :: JavaScript",
"Programming Language :: Python :: 3 :: Only",
"Programming Language :: Python :: 3.6",
"Programming Language :: Python :: 3.7",
"Programming Language :: Python :: 3.8",
"Topic :: Software Development :: Code Generators",
"Topic :: Utilities",
"Typing :: Typed",
],
)

View file

@ -0,0 +1,8 @@
from aws_cdk import core, aws_ec2
class VpcExampleStack(core.Stack):
def __init__(self, scope: core.Construct, id: str, **kwargs) -> None:
super().__init__(scope, id, **kwargs)
vpc = aws_ec2.Vpc(self, "MiaVPCTest", cidr="10.0.0.0/16", max_azs=3)