1 | # cloudform
|
2 | 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 | export 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 |
|
53 | See also [example/example.ts](https://github.com/bright/cloudform/blob/master/packages/cloudform/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 | Use `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 |
|
70 | The 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 |
|
72 | 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.
|
73 |
|
74 | All [Intrinsic Functions](http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/intrinsic-function-reference.html) are available within `Fn` namespace:
|
75 |
|
76 | ```typescript
|
77 | Fn.Base64(value: Value<string>)
|
78 | Fn.FindInMap(mapName: Value<string>, topLevelKey: Value<string>, secondLevelKey: Value<string>)
|
79 | Fn.GetAtt(logicalNameOfResource: Value<string>, attributeName: Value<string>)
|
80 | Fn.GetAZs(region?: Value<string>)
|
81 | Fn.ImportValue(sharedValueToImport: Value<any>)
|
82 | Fn.Join(delimiter: Value<string>, values: List<any>)
|
83 | Fn.Select(index: Value<number>, listOfObjects: List<any>)
|
84 | Fn.Split(delimiter: Value<string>, sourceString: Value<string>)
|
85 | Fn.Sub(string: Value<string>, vars [key: string]: Value<any> })
|
86 | Fn.Ref(logicalName: Value<string>)
|
87 |
|
88 | // condition functions
|
89 | Fn.And(condition: List<Condition>)
|
90 | Fn.Equals(left: any, right: any)
|
91 | Fn.If(conditionName: Value<string>, valueIfTrue: any, valueIfFalse: any)
|
92 | Fn.Not(condition: Condition)
|
93 | Fn.Or(condition: List<Condition>)
|
94 | ```
|
95 |
|
96 | All the [Pseudo Parameters](http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/pseudo-parameter-reference.html) are there, too:
|
97 |
|
98 | ```
|
99 | Ref.AccountId
|
100 | Ref.NotificationARNs
|
101 | Ref.NoValue
|
102 | Ref.Partition
|
103 | Ref.Region
|
104 | Ref.StackId
|
105 | Ref.StackName
|
106 | Ref.URLSuffix
|
107 | ```
|
108 |
|
109 | ## Licence
|
110 |
|
111 | [MIT](https://github.com/bright/cloudform/blob/master/LICENCE)
|