|
1 |
| -# cloudform.js |
| 1 | +# cloudform |
2 | 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 | +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) |
0 commit comments