Skip to content

Commit 56b9ccf

Browse files
author
adambar
committed
Readme, example fixes, version bump to 1.0
1 parent f485824 commit 56b9ccf

File tree

3 files changed

+195
-11
lines changed

3 files changed

+195
-11
lines changed

README.md

Lines changed: 168 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,169 @@
1-
# cloudform.js
1+
# cloudform
22
TypeScript-based imperative way to define AWS CloudFormation templates
3+
4+
[Read the introductory blog post](https://brightinventions.pl/blog/introducing-cloudform-tame-aws-cloudformation-templates/)
5+
6+
## Installation
7+
8+
`npm install --save-dev cloudform`
9+
10+
## Usage
11+
12+
1. Define your AWS CloudFormation template in a TypeScript file, for example `template.ts`:
13+
14+
```typescript
15+
import cloudform, {Fn, Refs, EC2, StringParameter, ResourceTag} from "cloudform"
16+
17+
cloudform({
18+
Description: 'My template',
19+
Parameters: {
20+
DeployEnv: new StringParameter({
21+
Description: 'Deploy environment name',
22+
AllowedValues: ['stage', 'production']
23+
})
24+
},
25+
Mappings: {
26+
DeploymentConfig: {
27+
stage: {
28+
InstanceType: 't2.small'
29+
},
30+
production: {
31+
InstanceType: 't2.large'
32+
}
33+
}
34+
},
35+
Resources: {
36+
VPC: new EC2.VPC({
37+
CidrBlock: NetworkingConfig.VPC.CIDR,
38+
EnableDnsHostnames: true,
39+
Tags: [
40+
new ResourceTag('Application', Refs.StackName),
41+
new ResourceTag('Network', 'Public'),
42+
new ResourceTag('Name', Fn.Join('-', [Refs.StackId, 'VPC']))
43+
]
44+
}),
45+
Instance: new EC2.Instance({
46+
InstanceType: Fn.FindInMap('DeploymentConfig', Fn.Ref('DeployEnv'), 'InstanceType'),
47+
ImageId: 'ami-a85480c7'
48+
}).dependsOn('VPC')
49+
}
50+
})
51+
```
52+
53+
See also [example/example.ts](https://github.com/bright/cloudform/blob/master/example/example.ts).
54+
55+
2\. Run `cloudform path/to/your/template.ts` to generate the CloudFormation template as JSON.
56+
57+
It makes sense to define it in your `npm` scripts and run within your build or deployment pipeline, for example:
58+
59+
```json
60+
"scripts"
61+
// ...
62+
"generate-cloudformation-template": "cloudform path/to/your/template > template.aws"
63+
}
64+
```
65+
66+
## API
67+
68+
The types are generated automatically from the [AWS-provided schema file](http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/cfn-resource-specification.html), so `cloudform` supports all the types available in AWS CloudFormation.
69+
70+
The simple convention is used – all the AWS types’ namespaces are available directly as exports from the `cloudform` package. All the resources within this package are available inside. This way `EC2.VPC` object from our example translates into `AWS::EC2::VPC` type we can find in [CloudFormation documentation](http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-ec2-vpc.html). All the properties also match one-to-one, including casing.
71+
72+
Supported namespaces are:
73+
74+
```
75+
ApiGateway
76+
ApplicationAutoScaling
77+
Athena
78+
AutoScaling
79+
Batch
80+
CertificateManager
81+
CloudFormation
82+
CloudFront
83+
CloudTrail
84+
CloudWatch
85+
CodeBuild
86+
CodeCommit
87+
CodeDeploy
88+
CodePipeline
89+
Cognito
90+
Config
91+
DAX
92+
DMS
93+
DataPipeline
94+
DirectoryService
95+
DynamoDB
96+
EC2
97+
ECR
98+
ECS
99+
EFS
100+
EMR
101+
ElastiCache
102+
ElasticBeanstalk
103+
ElasticLoadBalancing
104+
ElasticLoadBalancingV2
105+
Elasticsearch
106+
Events
107+
GameLift
108+
GuardDuty
109+
IAM
110+
IoT
111+
KMS
112+
Kinesis
113+
KinesisAnalytics
114+
KinesisFirehose
115+
Lambda
116+
Logs
117+
OpsWorks
118+
RDS
119+
Redshift
120+
Route53
121+
S3
122+
SDB
123+
SNS
124+
SQS
125+
SSM
126+
StepFunctions
127+
WAF
128+
WAFRegional
129+
WorkSpaces
130+
```
131+
132+
All [Intrinsic Tunctions](http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/intrinsic-function-reference.html) are available within `Fn` namespace:
133+
134+
```typescript
135+
Fn.Base64(value: Value<string>)
136+
Fn.FindInMap(mapName: Value<string>, topLevelKey: Value<string>, secondLevelKey: Value<string>)
137+
Fn.GetAtt(logicalNameOfResource: Value<string>, attributeName: Value<string>)
138+
Fn.GetAZs(region?: Value<string>)
139+
Fn.ImportValue(sharedValueToImport: Value<any>)
140+
Fn.Join(delimiter: Value<string>, values: List<any>)
141+
Fn.Select(index: Value<number>, listOfObjects: List<any>)
142+
Fn.Split(delimiter: Value<string>, sourceString: Value<string>)
143+
Fn.Sub(string: Value<string>, vars [key: string]: Value<any> })
144+
Fn.Ref(logicalName: Value<string>)
145+
146+
// condition functions
147+
Fn.And(condition: List<Condition>)
148+
Fn.Equals(left: any, right: any)
149+
Fn.If(conditionName: Value<string>, valueIfTrue: any, valueIfFalse: any)
150+
Fn.Not(condition: Condition)
151+
Fn.Or(condition: List<Condition>)
152+
```
153+
154+
All the [Pseudo Parameters](http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/pseudo-parameter-reference.html) are there, too:
155+
156+
```
157+
Ref.AccountId
158+
Ref.NotificationARNs
159+
Ref.NoValue
160+
Ref.Partition
161+
Ref.Region
162+
Ref.StackId
163+
Ref.StackName
164+
Ref.URLSuffix
165+
```
166+
167+
## Licence
168+
169+
[MIT](https://github.com/bright/cloudform/blob/master/LICENCE)

example/example.ts

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,41 @@
1-
import cloudform, {Fn, Refs, EC2} from "../index"
2-
import {StringParameter} from "../types/parameter"
3-
import {ResourceTag} from "../types/resource"
1+
import cloudform, {Fn, Refs, EC2, StringParameter, ResourceTag} from ".." // you should import from cloudform here instead
2+
3+
const NetworkingConfig = {
4+
VPC: {
5+
CIDR: '10.0.0.0/16'
6+
}
7+
}
8+
9+
const DeployEnv = Fn.Ref('DeployEnv')
410

511
cloudform({
612
Description: 'My template',
713
Parameters: {
814
DeployEnv: new StringParameter({
915
Description: 'Deploy environment name',
10-
AllowedValues: ['dev', 'stage', 'production']
16+
AllowedValues: ['stage', 'production']
1117
})
1218
},
1319
Mappings: {
14-
SubnetConfig: {
15-
VPC: {
16-
CIDR: '10.0.0.0/16'
20+
SomeGroup: {
21+
stage: {
22+
SomeValue: 'one'
23+
},
24+
production: {
25+
SomeValue: 'two'
1726
}
1827
}
1928
},
2029
Conditions: {
2130
FirstCondition: Fn.Equals(1, 2),
22-
TestCondition: Fn.And([{Condition: 'FirstCondition'}, Fn.Equals("a", "b")])
31+
TestCondition: Fn.And([
32+
{Condition: 'FirstCondition'},
33+
Fn.Equals(Fn.FindInMap('SomeGroup', DeployEnv, 'SomeValue'), 'three')
34+
])
2335
},
2436
Resources: {
2537
VPC: new EC2.VPC({
26-
CidrBlock: Fn.FindInMap('SubnetConfig', 'VPC', 'CIDR'),
38+
CidrBlock: NetworkingConfig.VPC.CIDR,
2739
EnableDnsHostnames: true,
2840
Tags: [
2941
new ResourceTag('Application', Refs.StackName),
@@ -42,5 +54,10 @@ cloudform({
4254
}
4355
}
4456
}
57+
},
58+
Outputs: {
59+
VPCIpv6CidrBlocks: {
60+
Value: Fn.GetAtt('VPC', 'Ipv6CidrBlocks')
61+
}
4562
}
4663
})

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "cloudform",
3-
"version": "0.0.13",
3+
"version": "1.0.0",
44
"description": "TypeScript-based imperative way to define AWS CloudFormation templates",
55
"main": "dist/index.js",
66
"scripts": {

0 commit comments

Comments
 (0)