Vous êtes sur la page 1sur 70

Using Parameters and Outputs to

Customize a Stack
R e u s e tem pla tes for multiple slightly
Overview different sta cks
A d d p a ra m eters to a tem p late
Integrate sta cks into ex isting
environ ments
U s e ou tp u ts to a c c e s s results from a
running sta ck
A l re a d y d o n e
- CloudFormation template including a
SSH bastion host environment
To do
- Customize stack for slightly different use
cases
Input and Output

Inp u t C lo ud F o rm ation O u tp u t

CF
S p e c i f y input Stack A c c e s s results
pa ra meters c o ns i de rs input
a n d returns
outputs
Parameters to Specify Input
A d a m wa nts to reuse his tem p late for
multiple S S H b a stion h ost setu p s
S m a ll c h a n g e s are n e e d e d d e p e n d i n g for
different setu p s
- Key Pair
- Instance Type
Reuse Template

CF
Input A Stack A

Template
CF
Input B Stack B
Parameters Section

{
"AWSTemplateFormatVersion": "2010-09-09",
"Description": "SSH Bastion Host",
"Parameters": {...},

"Resources": {...}
}
Defining a Parameter

"Parameters": {
"KeyPair": {
"Description": "A SSH key pair.",
"Type": "AWS::EC2::KeyPair::KeyName"

}
}
Defining a Parameter

"Parameters": {
"KeyPair": {
"Description": "A SSH key pair.",
"Type": "AWS::EC2::KeyPair::KeyName"

}
}
"InstanceType": {
"Description": “The instance type.",
"Type": "String"

Description
Describe your pa ra m eters to simplify filling them out later
Defining a Parameter

"Parameters": {
"KeyPair": {
"Description": "A SSH key pair.",
"Type": "AWS::EC2::KeyPair::KeyName"

}
}
String : "string"
Number : 1

List<Number> : [1,2,3]
CommaDelimitedList : "1,2,3"

Parameter Type
Genera l pa ra m eter t y p e s
AWS::EC2::Image::Id : "ami-b43503a4"
AWS::EC2::Instance::Id : "i-47cad982"
AWS::EC2::SecurityGroup::Id : "sg-6246b70c"
AWS::Route53::HostedZone::Id : "Z2VHZWU704GYI1"

Parameter Type
A W S pa ra m eter t y p e s
Referencing Parameters
"Parameters": {
"KeyPair": {...}

},
"Resources": {
"EC2Instance": {
"Properties": {

"KeyName": {"Ref": "KeyPair"}


}
}}
Referencing Parameters
"Parameters": {
"KeyPair": {...}

},
"Resources": {
"EC2Instance": {
"Properties": {

"KeyName": {"Ref": "KeyPair"}


}
}}
AllowedValues : ["a", "b", "c"]
AllowedPattern : "[a-z0-9]*"
MaxLength / MinLength : 1
MaxValue / MinValue : 2

Input Validation
S oftwa re developm ent 101 : validate your inputs
Validating Instance Type

"InstanceType": {
"Description": "The instance type.",
"Type": "String",

"AllowedValues": ["t2.nano", "t2.micro", "t2.small"]


}
Default Value

"InstanceType": {
"Description": "The instance type.",
"Type": "String",

"Default": "t2.nano"
}
Demo Walk - throug h template
- Input Parameters
- Validation
C reate sta ck ou t of tem p late b y u s in g
C o m m a n d L i n e Interface ( C L I )
S p e c i f y input pa ra m eters for sta ck
Outputs to Access Results from Stack
A d a m wa nts to integrate S S H b a stio n
server sta cks into ex istin g environment
H e n eed s to a c c e s s the results from a sta ck
to b e a b le to
- Update DNS record with a Public IP
- Add a Private IP to firewall configuration
Access Results of Stack

Template S t a c k returns O ut put s c a n b e us e d


defines o ut put s o ut put s after for integration with
sta c k wa s c re ate d other environments
Outputs Section

{
"AWSTemplateFormatVersion": "2010-09-09",
"Description": "SSH Bastion Host",
"Parameters": {...},

"Resources": {...},
"Outputs": {...}
}
Defining an Output

"Outputs": {
"SSHBastionHostPublicIp": {
"Description": "Public IP address ...",
"Value": {"Ref": "ElasticIP"}

}
}
Defining an Output

"Outputs": {
"SSHBastionHostPublicIp": {
"Description": "Public IP address ...",
"Value": {"Ref": "ElasticIP"}

}
}
Defining an Output

"Outputs": {
"SSHBastionHostPublicIp": {
"Description": "Public IP address ...",
"Value": {"Ref": "ElasticIP"}

}
}
"SSHBastionHostPublicIp": {
"Description": "Public IP address ...",
"Value": {"Ref": "ElasticIP"}

Default Resource Return Value


A c c e s s i b l e via built-in Ref function
Return Va lue d e p e n d s on the Re s o u rc e Ty p e
"SSHBastionHostPrivateIp": {
"Description": "Private IP address ...",

"Value": {"Fn::GetAtt": ["EC2Instance", "PrivateIp"]}


}

Specific Resource Return Value


A c c e s s i b l e via built-in Fn::GetAtt function
Different return va lues d e p e n d i n g on the Re s o u rc e Ty p e
Returning Public IP
"Resources": {
"ElasticIP": {...}
}
"Outputs": {
"SSHBastionHostPublicIp": {
"Description": "Public IP address ...",
"Value": {"Ref": "ElasticIP"}

}
}
Returning Public IP
"Resources": {
"ElasticIP": {...}
}
"Outputs": {
"SSHBastionHostPublicIp": {
"Description": "Public IP address ...",
"Value": {"Ref": "ElasticIP"}

}
}
Returning Private IP
"Resources": {
“EC2": {...}
}
"Outputs": {
"SSHBastionHostPrivateIp": {
"Description": "Private IP address...",

"Value": {"Fn::GetAtt": ["EC2", "PrivateIp"]}


}
}
Returning Private IP
"Resources": {
“EC2": {...}
}
"Outputs": {
"SSHBastionHostPrivateIp": {
"Description": "Private IP address...",

"Value": {"Fn::GetAtt": ["EC2", "PrivateIp"]}


}
}
Demo Walk - th rou g h template
- Defining Outputs
- Accessing return values from
resources
Get outputs from a running sta ck via C L I
Inputs
- Allows you to reuse a template in
slightly differnt use cases
Summary - Template defines possible Parameters
- Paramters have to be specifiedwhen
creating/updating a stack
Outputs
- Allows you to access results from a
stack
- Template defines outputs
- Outputs can be accessed after stack
was created/updated successfully
Using Parameters and Outputs to
Customize a Stack
R e u s e tem pla tes for multiple slightly
Overview different sta cks
A d d p a ra m eters to a tem p late
Integrate sta cks into ex isting
environ ments
U s e ou tp u ts to a c c e s s results from a
running sta ck
A l re a d y d o n e
- CloudFormation template including a
SSH bastion host environment
To do
- Customize stack for slightly different use
cases
Input and Output

Inp u t C lo ud F o rm ation O u tp u t

CF
S p e c i f y input Stack A c c e s s results
pa ra meters c o ns i de rs input
a n d returns
outputs
Parameters to Specify Input
A d a m wa nts to reuse his tem p late for
multiple S S H b a stion h ost setu p s
S m a ll c h a n g e s are n e e d e d d e p e n d i n g for
different setu p s
- Key Pair
- Instance Type
Reuse Template

CF
Input A Stack A

Template
CF
Input B Stack B
Parameters Section

{
"AWSTemplateFormatVersion": "2010-09-09",
"Description": "SSH Bastion Host",
"Parameters": {...},

"Resources": {...}
}
Defining a Parameter

"Parameters": {
"KeyPair": {
"Description": "A SSH key pair.",
"Type": "AWS::EC2::KeyPair::KeyName"

}
}
Defining a Parameter

"Parameters": {
"KeyPair": {
"Description": "A SSH key pair.",
"Type": "AWS::EC2::KeyPair::KeyName"

}
}
"InstanceType": {
"Description": “The instance type.",
"Type": "String"

Description
Describe your pa ra m eters to simplify filling them out later
Defining a Parameter

"Parameters": {
"KeyPair": {
"Description": "A SSH key pair.",
"Type": "AWS::EC2::KeyPair::KeyName"

}
}
String : "string"
Number : 1

List<Number> : [1,2,3]
CommaDelimitedList : "1,2,3"

Parameter Type
Genera l pa ra m eter t y p e s
AWS::EC2::Image::Id : "ami-b43503a4"
AWS::EC2::Instance::Id : "i-47cad982"
AWS::EC2::SecurityGroup::Id : "sg-6246b70c"
AWS::Route53::HostedZone::Id : "Z2VHZWU704GYI1"

Parameter Type
A W S pa ra m eter t y p e s
Referencing Parameters
"Parameters": {
"KeyPair": {...}

},
"Resources": {
"EC2Instance": {
"Properties": {

"KeyName": {"Ref": "KeyPair"}


}
}}
Referencing Parameters
"Parameters": {
"KeyPair": {...}

},
"Resources": {
"EC2Instance": {
"Properties": {

"KeyName": {"Ref": "KeyPair"}


}
}}
AllowedValues : ["a", "b", "c"]
AllowedPattern : "[a-z0-9]*"
MaxLength / MinLength : 1
MaxValue / MinValue : 2

Input Validation
S oftwa re developm ent 101 : validate your inputs
Validating Instance Type

"InstanceType": {
"Description": "The instance type.",
"Type": "String",

"AllowedValues": ["t2.nano", "t2.micro", "t2.small"]


}
Default Value

"InstanceType": {
"Description": "The instance type.",
"Type": "String",

"Default": "t2.nano"
}
Demo Walk - throug h template
- Input Parameters
- Validation
C reate sta ck ou t of tem p late b y u s in g
C o m m a n d L i n e Interface ( C L I )
S p e c i f y input pa ra m eters for sta ck
Outputs to Access Results from Stack
A d a m wa nts to integrate S S H b a stio n
server sta cks into ex istin g environment
H e n eed s to a c c e s s the results from a sta ck
to b e a b le to
- Update DNS record with a Public IP
- Add a Private IP to firewall configuration
Access Results of Stack

Template S t a c k returns O ut put s c a n b e us e d


defines o ut put s o ut put s after for integration with
sta c k wa s c re ate d other environments
Outputs Section

{
"AWSTemplateFormatVersion": "2010-09-09",
"Description": "SSH Bastion Host",
"Parameters": {...},

"Resources": {...},
"Outputs": {...}
}
Defining an Output

"Outputs": {
"SSHBastionHostPublicIp": {
"Description": "Public IP address ...",
"Value": {"Ref": "ElasticIP"}

}
}
Defining an Output

"Outputs": {
"SSHBastionHostPublicIp": {
"Description": "Public IP address ...",
"Value": {"Ref": "ElasticIP"}

}
}
Defining an Output

"Outputs": {
"SSHBastionHostPublicIp": {
"Description": "Public IP address ...",
"Value": {"Ref": "ElasticIP"}

}
}
"SSHBastionHostPublicIp": {
"Description": "Public IP address ...",
"Value": {"Ref": "ElasticIP"}

Default Resource Return Value


A c c e s s i b l e via built-in Ref function
Return Va lue d e p e n d s on the Re s o u rc e Ty p e
"SSHBastionHostPrivateIp": {
"Description": "Private IP address ...",

"Value": {"Fn::GetAtt": ["EC2Instance", "PrivateIp"]}


}

Specific Resource Return Value


A c c e s s i b l e via built-in Fn::GetAtt function
Different return va lues d e p e n d i n g on the Re s o u rc e Ty p e
Returning Public IP
"Resources": {
"ElasticIP": {...}
}
"Outputs": {
"SSHBastionHostPublicIp": {
"Description": "Public IP address ...",
"Value": {"Ref": "ElasticIP"}

}
}
Returning Public IP
"Resources": {
"ElasticIP": {...}
}
"Outputs": {
"SSHBastionHostPublicIp": {
"Description": "Public IP address ...",
"Value": {"Ref": "ElasticIP"}

}
}
Returning Private IP
"Resources": {
“EC2": {...}
}
"Outputs": {
"SSHBastionHostPrivateIp": {
"Description": "Private IP address...",

"Value": {"Fn::GetAtt": ["EC2", "PrivateIp"]}


}
}
Returning Private IP
"Resources": {
“EC2": {...}
}
"Outputs": {
"SSHBastionHostPrivateIp": {
"Description": "Private IP address...",

"Value": {"Fn::GetAtt": ["EC2", "PrivateIp"]}


}
}
Demo Walk - th rou g h template
- Defining Outputs
- Accessing return values from
resources
Get outputs from a running sta ck via C L I
Inputs
- Allows you to reuse a template in
slightly differnt use cases
Summary - Template defines possible Parameters
- Paramters have to be specifiedwhen
creating/updating a stack
Outputs
- Allows you to access results from a
stack
- Template defines outputs
- Outputs can be accessed after stack
was created/updated successfully