DEF(create_my_group):
try:
# Get default VPC ID
response = ec2_client.describe_vpcs()
vpc_id = response.get('Vpcs', [{}])[0].get('VpcId', '')
print("VPC-Id: ",vpc_id)
# Create a group with access only through SSH
security_con1 = ec2_client.create_security_group(GroupName='SSH_Access',VpcId=vpc_id,
Description="This will allow only SSH access to EC2.")
security_group_id1=security_con1['GroupId']
print("Created security group for EC2 Webserver.")
print("Now, attaching rules to this group......")
time.sleep(2)
# Create rule for SSH only access
rule = ec2_client.authorize_security_group_ingress(GroupId=security_group_id1,IpPermissions=[
{'IpProtocol': 'tcp','FromPort': 22,'ToPort': 22,'IpRanges': [{'CidrIp': '0.0.0.0/0'}]},
{'IpProtocol': 'tcp','FromPort': 443,'ToPort': 443,'IpRanges': [{'CidrIp': '0.0.0.0/0'}]}
]
)
print("Finished setting up EC2 security group.")
time.sleep(2)
# Create a group with access only through authorized group
security_con2 = ec2_client.create_security_group(GroupName='DB_Access',VpcId=vpc_id,
Description="This will allow only SSH access to DB.")
security_group_id2=security_con2['GroupId']
print("Created security group for DB-Webserver.")
print("Now, attaching rules to this group......")
time.sleep(2)
# Create access rule to access DB_access from users of SSH_Access group only
rule2 = ec2_client.authorize_security_group_ingress(GroupId=security_group_id2,
IpPermissions=[
{
'IpProtocol':'tcp','IpRanges':[],'FromPort':3306,'ToPort':3306,"UserIdGroupPairs": [{"GroupId": security_group_id1}]
}
]
)
print("Finished setting up DB security group.")
except ClientError as e:
print(e)
はIP権限でSGを添加し0.0.0.0/0にそれを変更しました。出来た。ありがとう!! – Anup