Example #1
0
def test_parameter__number():
    template = {}
    Parameter('abcde').type('type').max_value(100).min_value(10).to_template(
        template)
    assert_equal(
        template,
        {'abcde': {
            'Type': 'type',
            'MaxValue': '100',
            'MinValue': '10'
        }})
Example #2
0
def test_parameter__string():
    template = {}
    Parameter('abcde').type('type').allowed_pattern('pattern').max_length(
        100).min_length(10).to_template(template)
    assert_equal(
        template, {
            'abcde': {
                'Type': 'type',
                'AllowedPattern': 'pattern',
                'MaxLength': '100',
                'MinLength': '10'
            }
        })
Example #3
0
def test_parameter__any():
    template = {}
    Parameter('abcde') \
        .description('description') \
        .type('type') \
        .default('default') \
        .allowed_values(['value_1', 'value_2']) \
        .no_echo() \
        .to_template(template)
    assert_equal(
        template, {
            'abcde': {
                'Description': 'description',
                'Type': 'type',
                'Default': 'default',
                'AllowedValues': ['value_1', 'value_2'],
                'NoEcho': 'true'
            }
        })
Example #4
0
def generate():
    t = Template(description='see. https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/conditions-section-structure.html')

    meta = t.metadata(Metadatum('MetadataKey').attributes('name', 'value'))

    m = t.mappings(Mapping('RegionMap')
        .add_category('us-east-1').add_item('AMI', 'ami-7f418316').add_item('TestAZ', 'us-east-1a')
        .add_category('us-west-1').add_item('AMI', 'ami-951945d0').add_item('TestAZ', 'us-west-1a')
        .add_category('us-west-2').add_item('AMI', 'ami-16fd7026').add_item('TestAZ', 'us-west-2a')
    )

    p = t.parameters(Parameter('EnvType')
        .description('Environment type.')
        .type('String')
        .default('test')
        .allowed_values(['prod', 'test'])
        .constraint_description('must specify prod or test.')
    )

    c = t.conditions(Condition('CreateProdResources').expression(Intrinsics.fn_equals(Intrinsics.ref('EnvType'), 'prod')))

    r_ec2instance = t.resources(Resource('EC2Instance').type('AWS::EC2::Instance').properties([
        Attributes.of('ImageId', m.find_in_map(Pseudos.region(), 'AMI'))
    ]))

    r_new_volume = t.resources(Resource('NewVolume').type('AWS:EC2::Volume').properties([
        Attributes.of('Size', '100'),
        Attributes.of('AvailabilityZone', Intrinsics.get_att(r_ec2instance.name, 'AvailabilityZone'))
    ])).attributes('Condition', c.name)

    r_mount_point = t.resources(Resource('MountPoint').type('AWS::EC2::VolumeAttachment').properties([
        Attributes.of('InstanceId', Intrinsics.ref(r_ec2instance)),
        Attributes.of('VolumeId', Intrinsics.ref('NewVolume')),
        Attributes.of('Device', '/dev/sdh')
    ])).attributes('Condition', c.name)

    o = t.outputs(Output('VolumeId').value(Intrinsics.ref('NewVolume')).attributes('Condition', c.name))

    return t
Example #5
0
def generate():
    t = Template(description='td-agent Template')

    ami_id = t.parameters(
        Parameter('AmiId').description(
            'EC2 machine image ID of the sample server').type(
                'AWS::EC2::Image::Id'))

    instance_type = t.parameters(
        Parameter('InstanceType').description(
            'EC2 instance type of the sample server').type(
                'AWS::EC2::KeyPair::KeyName'))

    security_group_ids = t.parameters(
        Parameter('SecurityGroupIds').description(
            'List of security group IDs of the sample server').type(
                'List<AWS::EC2::SecurityGroup::Id>'))

    key_name = t.parameters(
        Parameter('KeyName').description(
            'Name of an existing EC2 key pair to enable SSH access to the sample server'
        ).type('AWS::EC2::KeyPair::KeyName'))

    subnet_id = t.parameters(
        Parameter('SubnetId').description(
            'Subnet ID which the sample server runs on').type(
                'AWS::EC2::Subnet::Id'))

    sample_server = t.resources(
        Resource('MongoDBServer').type('AWS::EC2::Instance').properties([
            Attributes.of('ImageId', ami_id),
            Attributes.of('InstanceType', instance_type),
            Attributes.of('SecurityGroupIds', security_group_ids),
            Attributes.of('KeyName', key_name),
            Attributes.of('SubnetId', subnet_id)
        ]))

    sample_server.add_property(
        UserData.from_files(
            [('files/x-shellscript', 'x-shellscript'),
             ('files/cloud-config', 'cloud-config')], {
                 'stack_id': Pseudos.stack_id(),
                 'resource_name': sample_server.name,
                 'region': Pseudos.region()
             }))

    sample_server.metadata(
        CfnInitMetadata.of([
            CfnInitMetadata.Init([
                CfnInitMetadata.ConfigSet('default', [
                    CfnInitMetadata.Config('SetupRepos').commands(
                        'import_td-agent_GPG-KEY',
                        'rpm --import https://packages.treasuredata.com/GPG-KEY-td-agent'
                    ),
                    CfnInitMetadata.Config('Install').packages('yum', 'dstat').
                    packages('yum', 'td-agent').commands(
                        'install_td-agent_plugin',
                        'td-agnet-gem install fluent-plugin-dstat fluent-plugin-map fluent-plugin-forest'
                    ),
                    CfnInitMetadata.Config('Configure').files(
                        '/etc/td-agent/td-agent.conf',
                        local_file_path='files/td-agent.conf',
                        mode='000644',
                        owner='root',
                        group='root'),
                    CfnInitMetadata.Config('Start').services(
                        'sysvinit',
                        'td-agent',
                        enabled=True,
                        ensure_running=True)
                ])
            ])
        ]))

    return t
Example #6
0
def generate():
    t = Template(description='MongoDB Template')

    ami_id = t.parameters(
        Parameter('AmiId').description(
            'EC2 machine image ID of the MongoDB server').type(
                'AWS::EC2::Image::Id'))

    instance_type = t.parameters(
        Parameter('InstanceType').description(
            'EC2 instance type of the MongoDB server').type(
                'AWS::EC2::KeyPair::KeyName'))

    security_group_ids = t.parameters(
        Parameter('SecurityGroupIds').description(
            'List of security group IDs of the MongoDB server').type(
                'List<AWS::EC2::SecurityGroup::Id>'))

    key_name = t.parameters(
        Parameter('KeyName').description(
            'Name of an existing EC2 key pair to enable SSH access to the MongoDB server'
        ).type('AWS::EC2::KeyPair::KeyName'))

    subnet_id = t.parameters(
        Parameter('SubnetId').description(
            'Subnet ID which the MongoDB server runs on').type(
                'AWS::EC2::Subnet::Id'))

    mongodb_server = t.resources(
        Resource('MongoDBServer').type('AWS::EC2::Instance').properties([
            Attributes.of('ImageId', ami_id),
            Attributes.of('InstanceType', instance_type),
            Attributes.of('SecurityGroupIds', security_group_ids),
            Attributes.of('KeyName', key_name),
            Attributes.of('SubnetId', subnet_id)
        ]))

    mongodb_server.add_property(
        UserData.from_files(
            [('files/x-shellscript', 'x-shellscript'),
             ('files/cloud-config', 'cloud-config')], {
                 'stack_id': Pseudos.stack_id(),
                 'resource_name': mongodb_server.name,
                 'region': Pseudos.region()
             }))

    mongodb_server.metadata(
        CfnInitMetadata.of([
            CfnInitMetadata.Init([
                CfnInitMetadata.ConfigSet('default', [
                    CfnInitMetadata.Config('SetupRepos').files(
                        '/etc/yum.repos.d/mongodb-org.3.2.repo',
                        local_file_path='files/mongodb-org-3.2.repo',
                        mode='00644',
                        owner='root',
                        group='root').
                    commands(
                        'import_mongodb_public_key',
                        'rpm --import https://www.mongodb.org/static/pgp/server-3.2.asc'
                    ),
                    CfnInitMetadata.Config('DownloadFromS3').files(
                        '/path/to',
                        source='https://s3.amazonaws.com/bucket/object',
                        mode='000644',
                        owner='root',
                        group='root',
                        authentication='s3credentials'),
                    CfnInitMetadata.Config('Install').packages(
                        'yum', 'mongodb-org-server').packages(
                            'yum', 'mongodb-org-shell').packages(
                                'yum', 'mongodb-org-tools'),
                    CfnInitMetadata.Config('Configure').files(
                        '/etc/mongod.conf',
                        local_file_path='files/mongod.conf',
                        mode='000644',
                        owner='root',
                        group='root').commands(
                            'make_data_directory',
                            'mkdir -p /data/db; chmod 777 /data/db'),
                    CfnInitMetadata.Config('Start').services(
                        'sysvinit',
                        'mongod',
                        enabled=True,
                        ensure_running=True)
                ])
            ]),
            CfnInitMetadata.Authentication('s3credentials',
                                           'S3').role_name('some-role')
        ]))

    return t
Example #7
0
def recipe(t):
    t.parameters(Parameter('EnvType').default('prod'), merge=True)