def test_update_tags(self): from troposphere_mate import ec2 # overwrite=False works tpl = Template() my_vpc = ec2.VPC("MyVPC", template=tpl, CidrBlock="10.0.0.0/16", Tags=Tags(Creator="Alice")) my_sg = ec2.SecurityGroup( "MySG", template=tpl, GroupDescription="My", GroupName="MySG", VpcId=Ref(my_vpc), ) my_subnet = ec2.Subnet( "MySubnet", template=tpl, CidrBlock="10.0.1.0/24", VpcId=Ref(my_vpc), ) def get_name(resource, project): if resource.resource_type == "AWS::EC2::SecurityGroup": return "{}/sg/{}".format(project, resource.GroupName) tpl.update_tags(dict(Project="my-project"), overwrite=False) tpl.update_tags(tags_dct=dict( Name=functools.partial(get_name, project="my-project"), Creator="Bob", ), overwrite=False) assert tags_list_to_dct( tpl.to_dict()["Resources"]["MyVPC"]["Properties"]["Tags"]) == dict( Project="my-project", Creator="Alice", ) assert tags_list_to_dct( tpl.to_dict()["Resources"]["MySG"]["Properties"]["Tags"]) == dict( Project="my-project", Name="my-project/sg/MySG", Creator="Bob", )
def create_template(app): """ :type app: AppConfig :rtype: Template """ template = Template() for repo in app.repos: if repo.repo_registry != AppConfig.Options.RepoRegistry.aws_ecr: continue ecr.Repository( f"Repository{camelcase(repo.repo_name)}", template=template, RepositoryName=repo.repo_name, LifecyclePolicy=ecr. LifecyclePolicy(LifecyclePolicyText=json.dumps({ "rules": [{ "rulePriority": 1, "description": "Expire images older than 14 days", "selection": { "tagStatus": "untagged", "countType": "sinceImagePushed", "countUnit": "days", "countNumber": repo.repo_aws_ecr_life_cycle_expire_days }, "action": { "type": "expire" } }] })), DeletionPolicy="Retain", ) common_tags = dict(ProjectName=app.app_name, ) template.update_tags(common_tags) return template
NetworkConfiguration=ecs.NetworkConfiguration( AwsvpcConfiguration=ecs.AwsvpcConfiguration( AssignPublicIp="ENABLED", SecurityGroups=[ Ref(sg_for_ecs), ], Subnets=[ config.PUBLIC_SUBNET_ID_AZ1.get_value(), config.PUBLIC_SUBNET_ID_AZ2.get_value(), ])), SchedulingStrategy="REPLICA", HealthCheckGracePeriodSeconds=30, DependsOn=[ sg_for_ecs, ecs_task_definition, elb_lb, elb_default_target_group, ], ) template.create_resource_type_label() # give all aws resource common tags common_tags = { "ProjectName": Ref(param_project_name), "ProjectNameSlug": Ref(param_project_name_slug), "Stage": Ref(param_stage), "EnvironmentName": Ref(param_env_name), } template.update_tags(common_tags)
EnvironmentVariables=EnvironmentVariables, PrivilegedMode=True, ImagePullCredentialsType="CODEBUILD", ), ServiceRole=code_build_service_role.iam_role_arn, BadgeEnabled=True, # Triggers=codebuild.ProjectTriggers( # Webhook=True, # FilterGroups=[ # [ # codebuild.WebhookFilter( # Type="EVENT", # Pattern="PUSH,PULL_REQUEST_CREATED,PULL_REQUEST_UPDATED", # ExcludeMatchedPattern=False, # ), # ] # ], # ), ) # --- Tags common_tags = { "Creator": "Sanhe Hu", "ProjectName": config.PROJECT_NAME_SLUG.get_value(), "Stage": config.STAGE.get_value(), "EnvironmentName": config.ENVIRONMENT_NAME.get_value(), } template.update_tags( tags_dct=common_tags ) template.create_resource_type_label()