815 def create_stack_aws_resources(stack,
819 elb_internal_security_group,
828 '''Create required AWS resources for given stack''' 829 verbose = kwargs.get(
'verbose',
False)
830 db_config = get_rds_config(region,
834 def get_vpc_subnets(vpc, prefix):
836 return [x[
'Value']
for x
in tags
837 if x[
'Key'] ==
'Name'][0]
838 return [x
for x
in vpc.subnets.all()
839 if name_tag(x.tags).startswith(prefix)]
841 def get_sg(name, region):
842 ec2_api = boto3.client(
'ec2', region_name=region)
843 return ec2_api.describe_security_groups(Filters=[{
844 'Name':
'group-name',
846 }])[
'SecurityGroups'][0]
848 def get_https_cert(name, region):
849 acm_api = boto3.client(
'acm', region_name=region)
850 certs = acm_api.list_certificates()[
'CertificateSummaryList']
852 if x[
'DomainName'] == name:
855 def get_hosted_zone_id(domain):
856 r53_api = boto3.client(
'route53')
857 response = r53_api.list_hosted_zones_by_name(
861 if len(response.get(
'HostedZones', [])) == 0:
862 raise SystemExit(
"Unable to find hosted zone by name")
863 return response[
'HostedZones'][0][
'Id']
865 elb_sg = get_sg(elb_security_group, region)
866 elb_int_sg = get_sg(elb_internal_security_group, region)
867 eni_sg = get_sg(eni_security_group, region)
868 ec2_res = boto3.resource(
'ec2', region_name=region)
869 vpc = ec2_res.Vpc(elb_sg[
'VpcId'])
870 elb_subnets = get_vpc_subnets(vpc, pub_subnet_prefix)
872 raise SystemExit(
"No usable subnets found")
873 eni_subnets = get_vpc_subnets(vpc, priv_subnet_prefix)
875 raise SystemExit(
"No usable private subnets found")
877 cert_name =
'%s.%s' % (https_cert_hostname, domain)
878 cert = get_https_cert(cert_name, region)
880 hosted_zone_id = get_hosted_zone_id(domain)
883 raise SystemExit(
"No HTTPS cert found for: %s" % cert_name)
886 'vpc_id': vpc.vpc_id,
887 'certificate_arn': cert[
'CertificateArn'],
888 'elb_security_groups': [elb_sg[
'GroupId']],
889 'elb_internal_security_groups': [elb_int_sg[
'GroupId']],
890 'eni_security_groups': [eni_sg[
'GroupId']],
891 'private_subnet_ids': [x.id
for x
in eni_subnets],
892 'subnet_ids': [x.id
for x
in elb_subnets],
893 'hosted_zone_id': hosted_zone_id,
896 if kwargs.get(
'create_database',
True):
897 db_creds = create_db(stack, db_config)
899 "host": db_config[
'host'],
900 "port": db_config[
'port'],
901 "name": db_creds[
'db'],
902 "user": db_creds[
'user'],
903 "password": db_creds[
'password'],
904 "rds_config": rds_config,
907 stack_cf_params = create_cloudformation_stack_params(stack, **kwargs)
909 stack_cf_id = cloudformation.create_cloudformation_stack(
914 if stack_cf_id
is None:
915 raise SystemExit(
"Unable to create Cloudformation Stack for Stack")
917 print(
"Created Cloudformation Stack: %s" % stack_cf_id)
919 stack.deploy = _deploy_dictionary_(stack)
921 stack.update_state(
'active')