Skip to content

Commit cf7304a

Browse files
authored
feat(event): do not create/update tags during delete (#16)
* process event requestType to not run when delete * add taints example to documentation * add examples with multiple tags combination * add docker tag documentation
1 parent 3195264 commit cf7304a

File tree

2 files changed

+92
-10
lines changed

2 files changed

+92
-10
lines changed

README.md

Lines changed: 81 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,18 @@ It's user responsability to specify the cluster the pools and the tags for each
1212

1313
## How to use
1414

15+
### Push to AWS ECR
16+
17+
Our pipelines publish which new release to the docker hub, but in order to call it from a lambda, it needs to be in the AWS account of your private ECR.
18+
19+
Example, but you can tag it with the name you want:
20+
21+
```bash
22+
docker pull ydata/aws-asg-tags-lambda:1.0.0
23+
docker tag ydata/aws-asg-tags-lambda:1.0.0 <your private ECR>/aws-asg-tags-lambda:1.0.0
24+
docker push <your private ECR>/aws-asg-tags-lambda:1.0.0
25+
```
26+
1527
### CloudFormation
1628

1729
The execution role, it's necessary to connect to the EKS and EC2 for the auto scaling groups
@@ -76,26 +88,85 @@ EKSASGTagLambdaInvoke:
7688
Region: !Ref AWS::Region
7789
ClusterName: "the EKS cluster name"
7890
CommonTags:
79-
- Name: "A Tag"
80-
Value: "A value for the tag"
91+
- Name: "ENVIRONMENT"
92+
Value: "dev"
93+
PropagateAtLaunch: true
8194
NodePools:
82-
- Name: "A node pool name"
95+
- Name: "system-nodepool"
8396
Tags:
84-
- Name: "Another Tag"
85-
Value: "A value for another tag"
86-
- Name: "Another pool name"
87-
Tags:
88-
- Name: "Another Tag"
89-
Value: "A value for another tag"
97+
- Name: 'k8s.io/cluster-autoscaler/node-template/taint/TAINT'
98+
Value: 'NoSchedule'
99+
PropagateAtLaunch: true
100+
- Name: 'k8s.io/cluster-autoscaler/node-template/label/LABEL'
101+
Value: 'LABEL_VALUE'
102+
PropagateAtLaunch: true
103+
- Name: "another-pool"
104+
105+
```
106+
107+
Both `CommonTags` and `Tags` of each NodePool are optional, but if you don't specify `CommonTags` neither `Tags` for each NodePool, it will not do anything.
108+
109+
Check the following examples for other valid combinations
90110

111+
An example with only `CommonTags`
112+
113+
```yaml
114+
EKSASGTagLambdaInvoke:
115+
Type: AWS::CloudFormation::CustomResource
116+
DependsOn: EKSASGTagLambdaFunction
117+
Version: "1.0"
118+
Properties:
119+
ServiceToken: !GetAtt EKSASGTagLambdaFunction.Arn
120+
StackID: !Ref AWS::StackId
121+
AccountID: !Ref AWS::AccountId
122+
Region: !Ref AWS::Region
123+
ClusterName: "the EKS cluster name"
124+
CommonTags:
125+
- Name: "ENVIRONMENT"
126+
Value: "prod"
127+
PropagateAtLaunch: true
128+
NodePools:
129+
- Name: "system-nodepool"
130+
- Name: "applications-nodepool"
91131
```
92132
133+
An example with only `Tags` for the NodePool
134+
135+
```yaml
136+
EKSASGTagLambdaInvoke:
137+
Type: AWS::CloudFormation::CustomResource
138+
DependsOn: EKSASGTagLambdaFunction
139+
Version: "1.0"
140+
Properties:
141+
ServiceToken: !GetAtt EKSASGTagLambdaFunction.Arn
142+
StackID: !Ref AWS::StackId
143+
AccountID: !Ref AWS::AccountId
144+
Region: !Ref AWS::Region
145+
ClusterName: "the EKS cluster name"
146+
NodePools:
147+
- Name: "system-nodepool"
148+
Tags:
149+
- Name: 'k8s.io/cluster-autoscaler/node-template/taint/TAINT'
150+
Value: 'NoSchedule'
151+
PropagateAtLaunch: true
152+
- Name: 'k8s.io/cluster-autoscaler/node-template/label/LABEL'
153+
Value: 'LABEL_VALUE'
154+
PropagateAtLaunch: true
155+
- Name: "application-nodepool"
156+
Tags:
157+
- Name: 'k8s.io/cluster-autoscaler/node-template/taint/TAINT'
158+
Value: 'NoSchedule'
159+
PropagateAtLaunch: true
160+
- Name: 'k8s.io/cluster-autoscaler/node-template/label/LABEL'
161+
Value: 'LABEL_VALUE'
162+
PropagateAtLaunch: true
163+
```
93164

94165
## TODO
95166
- [ ] Add generic context
96167
- [ ] Tests
97168
- [ ] Better Documentation
98-
- [ ] Support other methods of usage
169+
- [ ] Support other methods of invocation
99170

100171

101172
## About 👯‍♂️

Sources/CloudFormation/run.swift

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,13 @@ struct CloudFormationHandler: LambdaHandler {
2626
func handle(_ event: Event, context: LambdaContext) async throws -> Output {
2727
context.logger.info("running lambda with event \n\(event)")
2828

29+
switch event.requestType {
30+
case .create, .update: return try await createOrUpdate(event, context)
31+
case .delete: return try await delete(event, context)
32+
}
33+
}
34+
35+
private func createOrUpdate(_ event: Event, _ context: LambdaContext) async throws -> Output {
2936
guard let resourceProperties = event.resourceProperties else {
3037
return try await terminate(with: event, result: .failure(Error.missingResourceProperties))
3138
}
@@ -39,6 +46,10 @@ struct CloudFormationHandler: LambdaHandler {
3946
try await terminate(with: event, result: result)
4047
}
4148

49+
private func delete(_ event: Event, _ context: LambdaContext) async throws -> Output {
50+
try await terminate(with: event, result: LambdaResult<Error>.success(()))
51+
}
52+
4253
private func terminate<E: Swift.Error>(with event: Event, result: LambdaResult<E>) async throws {
4354
try await httpClient.terminateCloudFormationInvocation(event.responseURL, event: result.encode(for: event))
4455
}

0 commit comments

Comments
 (0)