Post

CloudFormation static Infrastructure

Create an AWS User to manage the infrastructure and give it permissions. Use the CloudFormation template to create static resources for storing templates, data, and logs related to the application.

static_resources_ecs.yml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
---
AWSTemplateFormatVersion : 2010-09-09
Description: Project backend infrastructure

# Set Parameters (values to pass to your template at runtime)
Parameters:
  ProjectName:
    Description: This name will be used for resource names, keyname and tagging.
    Type: String
    Default: App
  Environment:
    Description: Deployment environment.
    Type: String
    AllowedValues:
      - dev
      - prod
    Default: dev
  CreateS3Bucket:
      Type: String
      Default: 'true'
      AllowedValues:
      - 'true'
      - 'false'
      Description : Defines if S3 bucket will be created as part of this stack.

Conditions:
  IsDevelop: !Equals [!Ref Environment, dev]
  IsProduction: !Equals [!Ref Environment, prod]
  CreateBucket: !Equals [!Ref CreateS3Bucket, 'true']
  ReleasesBucketDevPolicy: !And
    - !Condition IsDevelop
    - !Condition CreateBucket

Resources:
### EFS
  ProjectAppSystemFiles:
    Type: 'AWS::EFS::FileSystem'
    Properties:
      AvailabilityZoneName: !Select [0, !GetAZs ""]
      BackupPolicy:
        Status: DISABLED
      PerformanceMode: generalPurpose
      Encrypted: false
      LifecyclePolicies:
        - TransitionToIA: AFTER_30_DAYS
        - TransitionToPrimaryStorageClass: AFTER_1_ACCESS
      FileSystemTags:
        - Key: Name
          Value: !Sub ${ProjectName}.FileSystem
    
### Logs
  LogGroup:
    Type: AWS::Logs::LogGroup
    Properties: 
      LogGroupName: !Sub '${ProjectName}-ECS-logs-${Environment}'
      RetentionInDays: 1

### ECR
  ECRepo:
    Type: AWS::ECR::Repository
    Properties: 
      LifecyclePolicy: 
        LifecyclePolicyText:
          '{
            "rules": [
              {
                "rulePriority": 1,
                "description": "Keep only five untagged images, expire all others",
                "selection": {
                    "tagStatus": "untagged",
                    "countType": "imageCountMoreThan",
                    "countNumber": 5
                },
                "action": {
                    "type": "expire"
                }
              }
            ]
          }'
        RegistryId: !Ref 'AWS::AccountId'
      RepositoryName: !Sub 'App-app-${Environment}-ecr'
      RepositoryPolicyText: 
        Version: "2012-10-17"
        Statement: 
          - 
            Sid: AllowPushPull
            Effect: Allow
            Principal: 
              AWS: 
                - !Sub 'arn:aws:iam::${AWS::AccountId}:assumed-role/****'
            Action: 
              - "ecr:GetDownloadUrlForLayer"
              - "ecr:BatchGetImage"
              - "ecr:BatchCheckLayerAvailability"
              - "ecr:PutImage"
              - "ecr:InitiateLayerUpload"
              - "ecr:UploadLayerPart"
              - "ecr:CompleteLayerUpload"
      Tags:
      - {Key: Project, Value: !Ref ProjectName}
      - {Key: Environment, Value: !Ref Environment}

### CloudFront Origin Access Identity
  CloudFrontOAI:
    Type: AWS::CloudFront::CloudFrontOriginAccessIdentity
    Properties:
      CloudFrontOriginAccessIdentityConfig:
        Comment: !Sub 'Identity for ${ProjectName} CloudFront for Project app'

### S3 buckets for deployment
  ReleasesBucket:
    Type: AWS::S3::Bucket
    DeletionPolicy: Retain
    Condition: CreateBucket
    Properties:
      BucketName: !Sub 'csi-resources-${Environment}'
      LifecycleConfiguration: 
        Rules: 
        - Id: !If [IsProduction, 120 day delete artifacts rule, 45 day delete artifacts rule]
          Prefix: !Sub '${ProjectName}.'
          Status: Enabled
          ExpirationInDays: !If [IsProduction, 120, 45]
      AccessControl: Private
      VersioningConfiguration: 
        Status: Suspended
      Tags:
      - {Key: Project, Value: !Ref ProjectName}
      - {Key: Environment, Value: !Ref Environment}

  ReleasesBucketPolicy: 
    Type: AWS::S3::BucketPolicy 
    Condition: ReleasesBucketDevPolicy
    Properties:
      Bucket: !Ref ReleasesBucket
      PolicyDocument: 
        Version: "2008-10-17"
        Statement:
          -
            Action:
              - s3:ListBucket
              - s3:GetObject
              - s3:PutObject
            Effect: Allow
            Resource:
              - !Sub 'arn:aws:s3:::${ReleasesBucket}'
              - !Sub 'arn:aws:s3:::${ReleasesBucket}/*'
            Principal:
              AWS:
                - !Sub 'arn:aws:iam::${AWS::AccountId}:root'

### S3 buckets for Frontend
  BucketFrontend:
    Type: AWS::S3::Bucket
    DeletionPolicy: Retain
    Condition: CreateBucket
    Properties:
      BucketName: !Sub 'csi-app-${Environment}'
      AccessControl: Private
      CorsConfiguration:
        CorsRules:
          - AllowedHeaders:
              - '*'
            AllowedMethods:
              - PUT
              - POST
              - DELETE
            AllowedOrigins:
              - '*'
            MaxAge: 0
          - AllowedHeaders:
              - '*'
            AllowedMethods:
              - GET
              - DELETE
            AllowedOrigins:
              - '*'
            MaxAge: 0
      VersioningConfiguration: 
        Status: Suspended
      PublicAccessBlockConfiguration:
        BlockPublicAcls: true
        BlockPublicPolicy: true
        IgnorePublicAcls: true
        RestrictPublicBuckets: true
      Tags:
      - {Key: Project, Value: !Ref ProjectName}  
      - {Key: Environment, Value: !Ref Environment}

  BucketPolicy:
    Type: AWS::S3::BucketPolicy
    Properties: 
      Bucket: !Ref BucketFrontend
      PolicyDocument: 
        Version: '2012-10-17'
        Statement:
        - Effect: Allow
          Principal:
            AWS: !Sub "arn:aws:iam::cloudfront:user/CloudFront Origin Access Identity ${CloudFrontOAI}"
          Action: 's3:GetObject'
          Resource: !Sub 'arn:aws:s3:::csi-app-${Environment}/*'

### S3 buckets for logging
  CFLogsBucket:
    Type: AWS::S3::Bucket
    DeletionPolicy: Retain
    Condition: CreateBucket
    Properties:
      BucketName: !Sub 'csi-cf-logs-${Environment}'
      AccessControl: Private
      VersioningConfiguration: 
        Status: Suspended
      Tags:
      - {Key: Project, Value: !Ref ProjectName}
      - {Key: Environment, Value: !Ref Environment}

### Parameters
  ProjectAppSystemFilesParameter:
      Type: AWS::SSM::Parameter
      Properties:
        Name: !Sub '${Environment}.CSIAppSystemFiles.App'
        Type: String
        Value: !Ref ProjectAppSystemFiles
        Description: SSM Parameter for SystemFiles

  LogGroupParameter:
      Type: AWS::SSM::Parameter
      Properties:
        Name: !Sub '${Environment}.LogGroup.App'
        Type: String
        Value: !Ref LogGroup
        Description: SSM Parameter for LogGroup

  ECRepoParameter:
    Type: AWS::SSM::Parameter
    Properties:
      Name: !Sub '${Environment}.ECRepo.App'
      Type: String
      Value: !Ref ECRepo
      Description: SSM Parameter for ECRepo

  ReleasesBucketParameter:
    Type: AWS::SSM::Parameter
    Properties:
      Name: !Sub '${Environment}.ReleasesBucket.App'
      Type: String
      Value: !Ref ReleasesBucket
      Description: SSM Parameter for ReleasesBucket

  BucketFrontendParameter:
    Type: AWS::SSM::Parameter
    Properties:
      Name: !Sub '${Environment}.BucketFrontend.App'
      Type: String
      Value: !Ref BucketFrontend
      Description: SSM Parameter for BucketFrontend

### Outputs
Outputs:
  ProjectAppSystemFiles:
    Description: EFS
    Value: !Ref ProjectAppSystemFiles
    Export:
      Name: ProjectAppSystemFiles

  BucketFrontend:
    Description: CloudFront Bucket
    Value: !Ref BucketFrontend
    Export:
      Name: ProjectBucketFrontend

  CloudFrontOAI:
    Description: CloudFront Bucket
    Value: !GetAtt CloudFrontOAI.Id
    Export:
      Name: ProjectCloudFrontOAI

This post is licensed under CC BY 4.0 by the author.

Comments powered by Disqus.