官术网_书友最值得收藏!

Security groups

Security groups work a bit like firewalls. All EC2 instances have a set of security groups assigned to them, and each security group contains rules to allow traffic to flow inbound (ingress) and/or outbound (egress).

For this exercise, we will create a small web application running on port tcp/3000. In addition, we want to be able to SSH into the instance, so we also need to allow inbound traffic to port tcp/22. We will create a simple security group to allow this, by performing the following steps:

  1. First, we need to find out our default virtual private cloud (VPC) ID. Despite being in a cloud environment, where the physical resources are shared by all AWS customers, there is still a strong emphasis on security. AWS segmented their virtual infrastructure using the concept of VPC. You can imagine this as being a virtual datacenter with its own network. The security groups that protect our EC2 instances are tied with subnets that in turn are tied to the network that the VPC provides:

To identify our VPC ID, we can run the following command:

    $ aws ec2 describe-vpcs
{ "Vpcs": [ { "VpcId": "vpc-4cddce2a", "InstanceTenancy": "default", "CidrBlockAssociationSet": [ { "AssociationId": "vpc-cidr-assoc-3c313154", "CidrBlock": "172.31.0.0/16", "CidrBlockState": { "State": "associated" } } ], "State": "available", "DhcpOptionsId": "dopt-c0be5fa6", "CidrBlock": "172.31.0.0/16", "IsDefault": true } ] }
  1. Now that we know the VPC ID (yours will be different), we can create our new security group, as follows:
    $ aws ec2 create-security-group \
        --group-name HelloWorld \
        --description "Hello World Demo" \
        --vpc-id vpc-4cddce2a
{ "GroupId": "sg-01864b4c" }
  1. By default, security groups allow all outbound traffic from the instance. We just need to open up SSH (tcp/22) and tcp/3000 for inbound traffic. We then need to input the following:
    $ aws ec2 authorize-security-group-ingress \
        --group-name HelloWorld \
        --protocol tcp \
        --port 22 \
        --cidr 0.0.0.0/0
    
    $ aws ec2 authorize-security-group-ingress \
        --group-name HelloWorld \
        --protocol tcp \
        --port 3000 \
        --cidr 0.0.0.0/0  
  1. We can now verify the change made using the following code, as the previous commands aren't verbose:
    $ aws ec2 describe-security-groups \
        --group-names HelloWorld \
        --output text
SECURITYGROUPS Hello World Demo sg-01864b4c HelloWorld
094507990803 vpc-4cddce2a IPPERMISSIONS 22 tcp 22 IPRANGES 0.0.0.0/0 IPPERMISSIONS 3000 tcp 3000 IPRANGES 0.0.0.0/0 IPPERMISSIONSEGRESS -1 IPRANGES 0.0.0.0/0

As expected, we opened up the traffic to the proper ports. If you know how to find your public IP, you can improve the SSH rule by replacing 0.0.0.0/0 with your IP/32 so that only you can try to SSH into that EC2 instance.

Using the aws cli --output option
By default, most of the commands will return a JSON output. AWS has a a certain number of options globally available. You can see them used a bit in this chapter. The first option is --output [json | text | table]:

主站蜘蛛池模板: 揭阳市| 合川市| 山西省| 松滋市| 龙江县| 平武县| 酒泉市| 威海市| 威远县| 建水县| 普兰县| 新沂市| 惠州市| 深圳市| 犍为县| 团风县| 长沙市| 安溪县| 衡东县| 光泽县| 昆山市| 五常市| 砚山县| 渑池县| 新泰市| 安阳县| 新安县| 庐江县| 宁安市| 门头沟区| 大庆市| 曲周县| 共和县| 惠州市| 女性| 渝中区| 海口市| 林周县| 育儿| 封开县| 岳普湖县|