|
@ -1,40 +1,54 @@ |
|
|
import boto3 |
|
|
import boto3 |
|
|
from botocore.exceptions import ClientError |
|
|
from botocore.exceptions import ClientError |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
################################################################################################ |
|
|
################################################################################################ |
|
|
# |
|
|
# |
|
|
# Configuration Parameters |
|
|
# Configuration Parameters |
|
|
# |
|
|
# |
|
|
################################################################################################ |
|
|
################################################################################################ |
|
|
|
|
|
|
|
|
|
|
|
# !!!!!!!! You cannot use RDS in AWS Ecucate Account !!!!!!!! |
|
|
|
|
|
|
|
|
region = 'eu-central-1' |
|
|
|
|
|
availabilityZone = 'eu-central-1b' |
|
|
|
|
|
vpc_id = 'vpc-eedd4187' |
|
|
|
|
|
imageId = 'ami-0cc293023f983ed53' |
|
|
|
|
|
instanceType = 't3.nano' |
|
|
|
|
|
keyName = 'srieger-pub' |
|
|
|
|
|
|
|
|
# place your credentials in ~/.aws/credentials, as mentioned in AWS Educate Classroom, |
|
|
|
|
|
# Account Details, AWC CLI -> Show (Copy and paste the following into ~/.aws/credentials) |
|
|
|
|
|
|
|
|
# if you only have one VPC, vpc_id can be retrieved using: |
|
|
|
|
|
# |
|
|
|
|
|
# response = ec2Client.describe_vpcs() |
|
|
|
|
|
# vpc_id = response.get('Vpcs', [{}])[0].get('VpcId', '') |
|
|
|
|
|
|
|
|
# changed to use us-east, to be able to use AWS Educate Classroom |
|
|
|
|
|
region = 'us-east-1' |
|
|
|
|
|
availabilityZone = 'us-east-1a' |
|
|
|
|
|
# region = 'eu-central-1' |
|
|
|
|
|
# availabilityZone = 'eu-central-1b' |
|
|
|
|
|
|
|
|
|
|
|
# AMI ID of Amazon Linux 2 image 64-bit x86 in us-east-1 (can be retrieved, e.g., at |
|
|
|
|
|
# https://console.aws.amazon.com/ec2/v2/home?region=us-east-1#LaunchInstanceWizard:) |
|
|
|
|
|
imageId = 'ami-0d5eff06f840b45e9' |
|
|
|
|
|
# for eu-central-1, AMI ID of Amazon Linux 2 would be: |
|
|
|
|
|
# imageId = 'ami-0cc293023f983ed53' |
|
|
|
|
|
|
|
|
|
|
|
# potentially change instanceType to t2.micro for "free tier" if using a regular account |
|
|
|
|
|
# for production, t3.nano seams better |
|
|
|
|
|
instanceType = 't2.nano' |
|
|
|
|
|
|
|
|
|
|
|
keyName = 'srieger-pub' |
|
|
|
|
|
|
|
|
################################################################################################ |
|
|
################################################################################################ |
|
|
# |
|
|
# |
|
|
# boto3 code |
|
|
|
|
|
|
|
|
# boto3 clients and resources (dependencies) |
|
|
# |
|
|
# |
|
|
################################################################################################ |
|
|
################################################################################################ |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
client = boto3.setup_default_session(region_name=region) |
|
|
client = boto3.setup_default_session(region_name=region) |
|
|
ec2Client = boto3.client("ec2") |
|
|
ec2Client = boto3.client("ec2") |
|
|
ec2Resource = boto3.resource('ec2') |
|
|
ec2Resource = boto3.resource('ec2') |
|
|
|
|
|
|
|
|
rdsClient = boto3.client("rds") |
|
|
rdsClient = boto3.client("rds") |
|
|
|
|
|
|
|
|
|
|
|
# if you only have one VPC, vpc_id can be retrieved using: |
|
|
|
|
|
response = ec2Client.describe_vpcs() |
|
|
|
|
|
vpc_id = response.get('Vpcs', [{}])[0].get('VpcId', '') |
|
|
|
|
|
# if you have more than one VPC, vpc_id should be specified, and code |
|
|
|
|
|
# top retrieve VPC id below needs to be commented out |
|
|
|
|
|
# vpc_id = 'vpc-eedd4187' |
|
|
|
|
|
|
|
|
subnet_id = ec2Client.describe_subnets( |
|
|
subnet_id = ec2Client.describe_subnets( |
|
|
Filters=[ |
|
|
Filters=[ |
|
|
{ |
|
|
{ |
|
@ -42,6 +56,12 @@ subnet_id = ec2Client.describe_subnets( |
|
|
} |
|
|
} |
|
|
])['Subnets'][0]['SubnetId'] |
|
|
])['Subnets'][0]['SubnetId'] |
|
|
|
|
|
|
|
|
|
|
|
################################################################################################ |
|
|
|
|
|
# |
|
|
|
|
|
# boto3 code to deploy the application |
|
|
|
|
|
# |
|
|
|
|
|
################################################################################################ |
|
|
|
|
|
|
|
|
print("Deleting old instance...") |
|
|
print("Deleting old instance...") |
|
|
print("------------------------------------") |
|
|
print("------------------------------------") |
|
|
|
|
|
|
|
|