File Coverage

blib/lib/CCfnX/MakeAMIBase.pm
Criterion Covered Total %
statement 23 24 95.8
branch 4 8 50.0
condition n/a
subroutine 5 5 100.0
pod 0 1 0.0
total 32 38 84.2


line stmt bran cond sub pod time code
1             package CCfnX::MakeAMIBase {
2 1     1   715 use Moose;
  1         3  
  1         9  
3             extends 'CCfn';
4 1     1   7870 use CCfnX::Shortcuts;
  1         2  
  1         7  
5 1     1   2822 use CCfnX::UserData;
  1         2  
  1         29  
6 1     1   446 use CCfnX::CreateAMIUserData;
  1         14  
  1         460  
7              
8             resource CfnUser => 'AWS::IAM::User', {
9             Path => '/',
10             };
11              
12             resource CfnPolicy => 'AWS::IAM::Policy', {
13             PolicyName => 'AbilityToGetMetadata',
14             Roles => [ Ref('CfnRole') ],
15             Users => [ Ref('CfnUser') ],
16             PolicyDocument => {
17             Statement => [ {
18             Effect => "Allow",
19             Action => "cloudformation:DescribeStackResource",
20             Resource => "*"
21             }, ]
22             },
23             };
24              
25             resource CfnRole => 'AWS::IAM::Role', {
26             Path => '/',
27             AssumeRolePolicyDocument => {
28             Statement => [ {
29             Effect => 'Allow',
30             Principal => { Service => [ 'ec2.amazonaws.com' ] },
31             Action => [ 'sts:AssumeRole' ]
32             } ]
33             },
34             };
35              
36             resource CfnInstanceProfile => 'AWS::IAM::InstanceProfile', {
37             Path => '/',
38             Roles => [ Ref('CfnRole') ]
39             };
40              
41             resource HostKeys => 'AWS::IAM::AccessKey', {
42             UserName => Ref('CfnUser')
43             };
44             resource CreateAMISG => 'AWS::EC2::SecurityGroup', {
45             GroupDescription => "Security Group for Creating an AMI",
46             SecurityGroupIngress => [ SGRule(22, '0.0.0.0/0') ],
47             };
48              
49             sub BUILD {
50 1     1 0 95 my $self = shift;
51 1         3 my $udata;
52 1 50       24 if (not defined $self->params->template) {
53 0         0 warn "You haven't passed templates to this deployment. Please make sure that you signal stack completion with cfn-signal -e 0 -r \"cfn-int setup complete\" '#-#WaitHandle#-#' if you're not in --devel mode";
54             } else {
55 1         18 $udata = CCfnX::CreateAMIUserData->new(files => $self->params->template, signal => (! $self->params->devel), os_family => $self->params->os_family);
56             }
57              
58 1         1649 my $kp = $self->params->keypair;
59 1 50       7 $self->addResource(
    50          
60             "Instance", 'AWS::EC2::Instance',
61             Tags => [ Tag('Name', Parameter('name')) ],
62             ImageId => $self->params->ami,
63             InstanceType => $self->params->instance_type,
64             SecurityGroups => [ Ref('CreateAMISG') ],
65             IamInstanceProfile => Ref('CfnInstanceProfile'),
66             ($kp) ? (KeyName => $kp) : (),
67             ($udata) ? (UserData => $udata) : (),
68             );
69              
70 1         11 $self->addOutput(
71             InstanceID => Ref('Instance'),
72             );
73 1         7 $self->addOutput(
74             InstanceAddress => GetAtt('Instance', 'PublicIp')
75             );
76              
77 1 50       21 if (not $self->params->devel){
78 1         4 $self->addResource(
79             "WaitCondition", 'AWS::CloudFormation::WaitCondition',
80             Handle => Ref('WaitHandle'),
81             Timeout => "14400"
82             );
83 1         5 $self->addResource(
84             "WaitHandle", 'AWS::CloudFormation::WaitConditionHandle'
85             );
86             }
87             }
88             }
89              
90             1;