UNPKG

4.29 kBMarkdownView Raw
1# cloudform
2TypeScript-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
121. Define your AWS CloudFormation template in a TypeScript file, for example `template.ts`:
13
14```typescript
15import cloudform, {Fn, Refs, EC2, StringParameter, ResourceTag} from "cloudform"
16
17export default 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
53See also [example/example.ts](https://github.com/bright/cloudform/blob/master/packages/cloudform/example/example.ts).
54
552\. Run `cloudform path/to/your/template.ts` to generate the CloudFormation template as JSON.
56
57It 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
66Use `cloudform --minify path/to/your/template.ts` if you want CloudForm to output minified JSON instead of formatted. It might be useful if you reach [CloudFormation template body size limitation](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/cloudformation-limits.html).
67
68## API
69
70The types are generated automatically from the [AWS-provided schema file](http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/cfn-resource-specification.html) throughout all the regions, so `cloudform` supports all the types available in AWS CloudFormation.
71
72The 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.
73
74All [Intrinsic Functions](http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/intrinsic-function-reference.html) are available within `Fn` namespace:
75
76```typescript
77Fn.Base64(value: Value<string>)
78Fn.FindInMap(mapName: Value<string>, topLevelKey: Value<string>, secondLevelKey: Value<string>)
79Fn.GetAtt(logicalNameOfResource: Value<string>, attributeName: Value<string>)
80Fn.GetAZs(region?: Value<string>)
81Fn.ImportValue(sharedValueToImport: Value<any>)
82Fn.Join(delimiter: Value<string>, values: List<any>)
83Fn.Select(index: Value<number>, listOfObjects: List<any>)
84Fn.Split(delimiter: Value<string>, sourceString: Value<string>)
85Fn.Sub(string: Value<string>, vars [key: string]: Value<any> })
86Fn.Ref(logicalName: Value<string>)
87
88// condition functions
89Fn.And(condition: List<Condition>)
90Fn.Equals(left: any, right: any)
91Fn.If(conditionName: Value<string>, valueIfTrue: any, valueIfFalse: any)
92Fn.Not(condition: Condition)
93Fn.Or(condition: List<Condition>)
94```
95
96All the [Pseudo Parameters](http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/pseudo-parameter-reference.html) are there, too:
97
98```
99Ref.AccountId
100Ref.NotificationARNs
101Ref.NoValue
102Ref.Partition
103Ref.Region
104Ref.StackId
105Ref.StackName
106Ref.URLSuffix
107```
108
109## Licence
110
111[MIT](https://github.com/bright/cloudform/blob/master/LICENCE)