aws(学习笔记第三十四课) dockerized-app with asg-alb
- 使用
cdk
生成dockerized-app
并使用AutoScalingGroup
和ApplicationLoaderBalancer
学习内容:
- 使用
cdk
生成dockerized-app
并使用AutoScalingGroup
和ApplicationLoaderBalancer
- 在
AutoScalingGroup
中使用efs
以及RDS
1. 整体架构
1.1 代码链接
1.2 代码手动修改部分
这里的代码没有完全实现是理想的部署就能运行,需要修改几个地方。
1.2.1 rds_stack.py
修改instance_type=ec2.InstanceType.of这里的参数。不修改的话创建数据库运行失败。
修改后的代码:
from aws_cdk import (
aws_rds as rds,
aws_ec2 as ec2,
RemovalPolicy, Stack
)
from constructs import Construct
class RDSStack(Stack):
def __init__(self, scope: Construct, id: str, props, **kwargs) -> None:
super().__init__(scope, id, **kwargs)
# Creates a security group for AWS RDS
sg_rds = ec2.SecurityGroup(
self,
id="sg_rds",
vpc=props['vpc'],
security_group_name="sg_rds"
)
# Adds an ingress rule which allows resources in the VPC's CIDR
# to access the database.
sg_rds.add_ingress_rule(
peer=ec2.Peer.ipv4("10.0.0.0/16"),
connection=ec2.Port.tcp(3306)
)
# Master username is 'admin' and database password is automatically
# generated and stored in AWS Secrets Manager
my_sql = rds.DatabaseInstance(
self, "RDS",
engine=rds.DatabaseInstanceEngine.mysql(
version=rds.MysqlEngineVersion.VER_8_0_39
),
vpc=props['vpc'],
port=3306,
instance_type=ec2.InstanceType.of(
ec2.InstanceClass.R7G,
ec2.InstanceSize.LARGE,
),
removal_policy=RemovalPolicy.DESTROY,
security_groups=[sg_rds]
)
1.2.2 efs_stack.py
修改了efs
创建和security group
的创建顺序
修改后的代码:
from aws_cdk import (
aws_efs as efs,
aws_ec2 as ec2,
Stack
)
from constructs import Construct
class StorageStack(Stack):
def __init__(self, scope: Construct, id: str, props, **kwargs) -> None:
super().__init__(scope, id, **kwargs)
sg_efs = ec2.SecurityGroup(
self,
id="sg_efs",
vpc=props['vpc'],
security_group_name="sg_efs"
)
sg_efs.add_ingress_rule(
peer=ec2.Peer.ipv4