The optional Mappings section of cloudformation template can be used to declare variables. It is like python dictionary. You use the "FindInMap" intrinsic function to retrieve values. For e.g.
Mappings:
Function:
SocialMediaMLFunction:
S3Bucket: solutions
S3Key: ai-driven-social-media-dashboard/v1.0.0/socialmediafunction.zip
AddTriggerForFunction:
S3Bucket: solutions
S3Key: ai-driven-social-media-dashboard/v1.0.0/addtriggerfunction.zip
Code:
EC2Twitter:
S3Bucket: solutions
S3Key: ai-driven-social-media-dashboard/v1.0.0/ec2_twitter_reader.tar
If you want to refer to socialmediafunction.zip file along with it's path, then use...
S3Key: !FindInMap [ Function, SocialMediaMLFunction, S3Key]
And this statement will generate the URL that can be used to download the file...
EC2TwitterCode: !Join ['', ['https://s3.', !Ref 'AWS::Region', '.amazonaws.com/', !Join ['-', [!FindInMap [ Code, EC2Twitter, S3Bucket], !Ref 'AWS::Region']], '/', !FindInMap [ Code, EC2Twitter, S3Key]]]
The output will look something like this...
https://s3.us-east-1.amazonaws.com/solutions-us-east-1/ai-driven-social-media-dashboard/v1.0.0/ec2_twitter_reader.tar
_____
Here is another example:
Value: !FindInMap [RegionAndInstanceTypeToAMIID, !Ref "AWS::Region", !Ref EnvironmentType]
If your current region is us-east-1 and if the user has selected "test" environment as a parameter while creating the template, then the value returned will be "ami-8ff710e2" from this mapping:
Mappings:
RegionAndInstanceTypeToAMIID:
us-east-1:
test: "ami-8ff710e2"
prod: "ami-f5f41398"
us-west-2:
test: "ami-eff1028f"
prod: "ami-d0f506b0"
Labels: aws_cloudformation
The following code will initiate a Linux instance of type m3.medium using spot pricing and associate it to IP address 13.228.39.49 Make sure to use your own elastic IP address and key. Do not forget to change the access_key and secret_key parameters.
!wget https://raw.githubusercontent.com/shantanuo/easyboto/master/easyboto.py
import easyboto
dev=easyboto.connect('access_key', 'secret_key')
dev.placement='us-east-1a'
dev.myaddress='13.228.39.49'
dev.key='dec15abc'
dev.MAX_SPOT_BID= '2.9'
dev.startEc2Spot('ami-0323c3dd2da7fb37d', 'm3.medium')
This will return the instance id and the ssh command that you can use to connect to your instance. The output will look something like...
job instance id: i-029a926e68118d089
ssh -i dec15a.pem ec2-user@13.228.39.49
You can list all instances along with their details like launch time, image_id and save the results as pandas dataframe using showEc2 method like this...
df=dev.showEc2()
Now "df" is a pandas dataframe object. You can sort or groupby the instances just like an excel sheet.
You can delete the instance by providing the instance ID that was generated in the first step using deleteEc2 method.
dev.deleteEc2('i-029a926e68118d089')
_____
You can also use cloudformation template for this purpose. Visit the following link and look for "Linux EC2 Instance on SPOT" section.
https://github.com/shantanuo/cloudformation
Click on "Launch Stack" button. It is nothing but GUI for the python code mentioned above. You will simply have to submit a form for the methods like key and IP address.
Labels: aws, aws_cloudformation, boto, usability
CloudFormation was released in February 2011 as a service offering for Infrastructure as Code (IaC). This tool relies on declarative blueprint documents known as Templates. While initially, template could only be composed in JSON, several years later in 2016, support for YAML was added.
Template Anatomy
Templates supports top level structure using the following sections.
1. AWSTemplateFormatVersion
2. Description
3. Metadata
4. Parameters
5. Mappings
6. Conditions
7. Transform
8. Resources
9. Outputs
Among this list, Resources is the only mandatory section. After all, the goal is to provision AWS resources. The collection of resources provisioned by a given template is called a CloudFormation Stack and area treated as a single unit.
Read more: https://sysadvent.blogspot.com/2019/12/day-16-evolution-of-cloudformation.html
Labels: aws, aws_cloudformation
Writing cloudformation code can be very difficult for learners. Here is a "Recorder" that will watch your browser activity and convert it to CloudFormation/Terraform templates. Neat!
https://github.com/iann0036/AWSConsoleRecorder
_____
Generate CloudFormation / Terraform / Troposphere templates from your existing AWS resources
https://former2.com
Source code:
https://github.com/iann0036/former2
Labels: aws, aws_cloudformation, usability
1) Deploy Lambda function, called "variableSubstitution":
import json
def variable_substitution(event, context):
context = event['templateParameterValues']
fragment = walk(event['fragment'], context)
resp = {
'requestId': event['requestId'],
'status': 'success',
'fragment': fragment
}
return resp
def walk(node, context):
if isinstance(node, dict):
return { k: walk(v, context) for k, v in node.items() }
elif isinstance(node, list):
return [walk(elem, context) for elem in node]
elif isinstance(node, str):
return node.format(**context)
else:
return node
2) Create a Macro called "vS" to refer to the Lambda Function:
Resources:
CompanyDefaultsMacro:
Type: AWS::CloudFormation::Macro
Properties:
Name: vS
FunctionName: variableSubstitution
3) Use the Macro in Transform:
Transform:
- vS
Parameters:
stage:
Type: String
Default: dev
AllowedValues:
- dev
- staging
- prod
Resources:
MySNSTopic:
Type: AWS::SNS::Topic
Properties:
TopicName: "MyTopic-{stage}" # <-- look="" ma="" p="" python="" templating="">
-->
Labels: aws, aws_cloudformation, aws_lambda
The following template creates one AWS Systems Manager parameter resource and consumes the TTL stack as a nested resource, passing its name as a reference and the TTL as 5 minutes. When the TTL has elapsed, A lambda function called "DeleteCFNLambda" will delete the main stack as well as clean up the nested stack.
AWSTemplateFormatVersion: '2010-09-09'
Description: Demo stack, creates one SSM parameter and gets deleted after 5 minutes.
Resources:
DemoParameter:
Type: "AWS::SSM::Parameter"
Properties:
Type: "String"
Value: "date"
Description: "SSM Parameter for running date command."
AllowedPattern: "^[a-zA-Z]{1,10}$"
DeleteAfterTTLStack:
Type: "AWS::CloudFormation::Stack"
Properties:
TemplateURL: 'https://datameetgeobkup.s3.amazonaws.com/cftemplates/cfn-stack-ttl.yaml'
Parameters:
StackName: !Ref 'AWS::StackName'
TTL: '5'
The External template that will create a lambda function is available here...
https://github.com/aws-quickstart/quickstart-examples/blob/master/samples/cloudformation-stack-ttl/templates/cloudformation-stack-ttl.yaml
Source: https://aws.amazon.com/blogs/infrastructure-and-automation/scheduling-automatic-deletion-of-aws-cloudformation-stacks/
Labels: aws, aws_cloudformation
This cloudformation template will create an elastic instance that can be used for testing.
It will not have a password and will be accessible from any IP address. It means unsecure but good enough for testing with dummy data.
Resources:
ElasticsearchDomain:
Type: AWS::Elasticsearch::Domain
Properties:
DomainName: "testes"
ElasticsearchClusterConfig:
InstanceCount: "1"
InstanceType: "m3.medium.elasticsearch"
AccessPolicies:
Statement:
-
Effect: "Allow"
Principal:
AWS: "*"
Action: "es:*"
Resource: "arn:aws:es:us-east-1:${AWS::AccountId}:domain/testes/*"
Labels: aws, aws_cloudformation, elastic, usability