栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

cdk快捷指令

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

cdk快捷指令

mkdir unicorn&& cd unicorn
cdk init app -l python
pip install -r requirements.txt
pip install aws-cdk.aws_ec2
pip install aws-cdk.aws_autoscaling
pip install aws-cdk.aws_elasticloadbalancingv2
cdk ls
cdk synth
cdk deploy
mkdir userdata

以下内容写入/userdata/data.sh

#!/bin/bash
yum update -y
amazon-linux-extras install -y lamp-mariadb10.2-php7.2 php7.2
yum install -y httpd mariadb-server
systemctl start httpd
systemctl enable httpd
usermod -a -G apache ec2-user
chown -R ec2-user:apache /var/www
chmod 2775 /var/www
find /var/www -type d -exec chmod 2775 {} ;
find /var/www -type f -exec chmod 0664 {} ;
echo "" > /var/www/html/phpinfo.php

以下内容写入unicorn/unicorn/unicorn_stack.py

from aws_cdk import core as cdk
from aws_cdk import aws_ec2 as ec2 
from aws_cdk import aws_autoscaling as autoscaling
from aws_cdk import aws_elasticloadbalancingv2 as elb
# For consistency with other languages, `cdk` is the preferred import name for
# the CDK's core module.  The following line also imports it as `core` for use
# with examples from the CDK Developer's Guide, which are in the process of
# being updated to use `cdk`.  You may delete this import if you don't need it.
from aws_cdk import core
#定义实例类型
ec2_type = "t2.micro"
#定义密钥
key_name = "hello"

#导入用户数据文件
with open("./userdata/data.sh") as filesh:
    user_data = filesh.read()
    
    
class UnicornStack(cdk.Stack):

    def __init__(self, scope: cdk.Construct, construct_id: str, **kwargs) -> None:
        super().__init__(scope, construct_id, **kwargs)

        # The code that defines your stack goes here
        
        
        self.testVpc = ec2.Vpc(self,"myVpc",
            
            max_azs = 2,
            cidr = "10.0.0.0/16",
            subnet_configuration = [ec2.SubnetConfiguration(
                subnet_type = ec2.SubnetType.PUBLIC,
                name = "public",
                cidr_mask = 24
                ),ec2.SubnetConfiguration(
                subnet_type = ec2.SubnetType.PRIVATE,
                name = "private",
                cidr_mask = 24
                ),ec2.SubnetConfiguration(
                subnet_type = ec2.SubnetType.ISOLATED,
                name = "DB",
                cidr_mask = 24
                )
            ],
            nat_gateways = 2
        )
            
        self.sgalb = ec2.SecurityGroup(self,"sg_alb",
            vpc = self.testVpc,
            security_group_name = "sg_elb",
            allow_all_outbound = True
        )
        
        self.sgalb.connections.allow_from_any_ipv4(ec2.Port.tcp(80))
            
        self.sgdemo = ec2.SecurityGroup(self,"sg_demo",
            vpc = self.testVpc,
            security_group_name = "sg_demo",
            allow_all_outbound = True)
            
        ami_linux = ec2.MachineImage.latest_amazon_linux(
            #选择第2代亚马逊linux
            generation = ec2.AmazonLinuxGeneration.AMAZON_LINUX_2,
            #选择linux版本有 minimal 和 standard 两种
            edition = ec2.AmazonLinuxEdition.STANDARD,
            #选择虚拟化类型 有 HVM 和 PV ,可不配置默认HVM
            virtualization = ec2.AmazonLinuxVirt.HVM,
            #选择存储类型 EBS 和 GENERAL_PURPOSE
            storage = ec2.AmazonLinuxStorage.EBS
            )
            
        alb = elb.ApplicationLoadBalancer(self,"helloALB",
            vpc = self.testVpc,
            security_group=self.sgalb,
            internet_facing=True,
            load_balancer_name="helloALB"
            )
        listener = alb.add_listener("my80",
            port=80,
            open=True
        )
        
        
        
        #创建alb目标组
        listener.add_targets("addTargetGroup",
            protocol = elb.ApplicationProtocol.HTTP,
            port=80,
            
           
            )
        
        #输出alb的dns地址
        core.CfnOutput(self,"Output",
            value=alb.load_balancer_dns_name
        )

cdk deploy

备用代码

from aws_cdk import core
from aws_cdk import (aws_ec2 as ec2,
    aws_autoscaling as autoscaling,
    aws_elasticloadbalancingv2 as elb
    )

#定义实例类型
ec2_type = "t2.micro"
#定义密钥
key_name = "hello"
#CLI 创建密钥 aws ec2 create-key-pair --key-name hello --query 'hello' --output text > hello.pem  	

#导入用户数据文件
with open("./userdata/data.sh") as filesh:
    user_data = filesh.read()


class albec2Stack(core.Stack):

    def __init__(self, scope: core.Construct, id: str,vpc,sg_alb,sg_demo, **kwargs) -> None:
        super().__init__(scope, id, **kwargs)

        # The code that defines your stack goes here
        
        #选择AMI镜像    
        ami_linux = ec2.MachineImage.latest_amazon_linux(
            
            #选择第2代亚马逊linux
            generation = ec2.AmazonLinuxGeneration.AMAZON_LINUX_2,
            #选择linux版本有 minimal 和 standard 两种
            edition = ec2.AmazonLinuxEdition.STANDARD,
            #选择虚拟化类型 有 HVM 和 PV ,可不配置默认HVM
            virtualization = ec2.AmazonLinuxVirt.HVM,
            #选择存储类型 EBS 和 GENERAL_PURPOSE
            storage = ec2.AmazonLinuxStorage.EBS
            )
        
        
        #创建alb
        alb = elb.ApplicationLoadBalancer(self,"helloALB",
            vpc =vpc,
            security_group=sg_alb,
            internet_facing=True,
            load_balancer_name="helloALB"
            )
        
   
        #添加新的alb监听端口80
        listener = alb.add_listener("my80",
            port=80,
            open=True
        )
           
        #创建 AutoScaling组
        asg = autoscaling.AutoScalingGroup(self,"myautoscaling",
            vpc = vpc,
            #实例启动在私网子网
            vpc_subnets = ec2.SubnetSelection(subnet_type=ec2.SubnetType.PRIVATE),
            #实例类型
            instance_type=ec2.InstanceType(instance_type_identifier=ec2_type),
            #实例镜像
            machine_image = ami_linux,
            #实例密钥
            key_name =key_name,
            security_group=sg_demo,
            #实例用户数据
            user_data=ec2.UserData.custom(user_data),
            #需求2实例
            desired_capacity=2,
            #最小与最大弹性伸缩
            min_capacity=1,
            max_capacity=4,
            instance_name=demo
            )
     
        
        #创建alb目标组
        listener.add_targets("addTargetGroup",
            protocol = elb.ApplicationProtocol.HTTP,
            port=7777,
            
            #目标组为AutoScaling组
            targets=[asg]
            )
        
        #输出alb的dns地址
        core.CfnOutput(self,"Output",
            value=alb.load_balancer_dns_name
        )
© 2021 GitHub, Inc.
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/283406.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号