Skip to content

Commit 8b75215

Browse files
authored
Merge pull request #210 from NetApp/add_cf_to_auto_alarm
Add a CloudFormation to auto_set_fsxn_auto_grow
2 parents ebbbb23 + d8c08c4 commit 8b75215

File tree

5 files changed

+918
-65
lines changed

5 files changed

+918
-65
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
---
2+
# Copyright (c) NetApp, Inc.
3+
# SPDX-License-Identifier: Apache-2.0
4+
5+
name: "Update Cloudformation Template"
6+
7+
on:
8+
pull_request:
9+
paths:
10+
- 'Management-Utilities/auto_set_fsxn_auto_grow/set_fsxn_volume_auto_grow.py'
11+
push:
12+
paths:
13+
- 'Management-Utilities/auto_set_fsxn_auto_grow/set_fsxn_volume_auto_grow.py'
14+
branches:
15+
- main
16+
17+
jobs:
18+
update-Cloudformation-Template:
19+
runs-on: ubuntu-latest
20+
permissions:
21+
# Give the default GITHUB_TOKEN write permission to commit and push the
22+
# added or changed files to the repository.
23+
contents: write
24+
25+
steps:
26+
- name: Checkout pull request
27+
uses: actions/checkout@v4
28+
with:
29+
ref: ${{ github.event.pull_request.head.ref }}
30+
31+
- name: Update the Cloudformation Template
32+
shell: bash
33+
working-directory: Management-Utilities/auto_set_fsxn_auto_grow
34+
run: ./update_auto_set_fsxn_auto_grow_CF_Template
35+
36+
- name: Commit the changes
37+
uses: stefanzweifel/git-auto-commit-action@v5

Management-Utilities/auto_set_fsxn_auto_grow/README.md

Lines changed: 143 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -2,57 +2,158 @@
22

33
## Introduction
44
This sample shows one way to mitigate the issue of not being able to set the auto size mode
5-
on an FSxN volume when creating it from the AWS console or API. It does this by providing
5+
on an FSx for ONTAP volume when creating it from the AWS console or API. It does this by providing
66
a Lambda function that will set the mode for you, and instructions on how to set up a
77
CloudWatch event to trigger the Lambda function whenever a volume is created. With this
88
combination it ensures that all volumes are effectively created with the auto size mode
99
set up the way you want for all volumes.
1010

11-
## Set Up
12-
There are just a few things you have to do to set this up:
11+
Note that a CloudWatch event is not created when a volume is created directly from the
12+
ONTAP side, either using the ONTAP CLI, System Manager, or REST API. So, it is assumed
13+
if you are creating them that way, that you will set them with the auto size mode set
14+
the way you want.
1315

14-
### Create secrets in AWS Secrets Manager
16+
Since the Lambda function has to communicate with the FSx for ONTAP management
17+
endpoint, it has to run within a VPC that has that connectivity. Because of the way
18+
AWS allows a Lambda function to run within a VPC, it will not have access to the Internet
19+
even if normally it would from that subnet. Therefore, you will have to set up
20+
VPC endpoints for the AWS services that the Lambda function uses. This includes:
21+
- FSx
22+
- AWS Secrets Manager
23+
- DynamoDB if you are using it to store the secrets table
24+
25+
If you use the CloudFormation template provided in this repository to deploy the sample
26+
you will be given the option to have it create these service endpoints for you. If you are
27+
setting up the Lambda function manually, you will have to create these endpoints yourself.
28+
29+
Note that you can only have one service endpoint per service per VPC. So, don't attempt
30+
to add one if one already exists for the VPC you are going to run the Lambda function in.
31+
32+
The way this script authenticates to the FSx for ONTAP management endpoint is by using
33+
the credentials stored in AWS Secrets Manager. Since it can manage multiple FSxN file
34+
systems a table is used to specify which secret to use for each file system. This `secretsTable`
35+
can either be stored in a DynamoDB table, or just hard coded in the source code of the
36+
Lambda function. The schema for the `secretsTable` is as follows:
37+
```json
38+
[
39+
{"fsxId": "fs-XXXXXXXXXXXXXXXXX", "secretName": "fsxn-credentials", "usernameKey": "username", "passwordKey": "password"},
40+
{"fsxId": "fs-XXXXXXXXXXXXXXXXX", "secretName": "fsxn-credentials", "usernameKey": "username", "passwordKey": "password"},
41+
{"fsxId": "fs-XXXXXXXXXXXXXXXXX", "secretName": "fsxn-credentials", "usernameKey": "username", "passwordKey": "password"},
42+
{"fsxId": "fs-XXXXXXXXXXXXXXXXX", "secretName": "fsxn-credentials", "usernameKey": "username", "passwordKey": "password"}
43+
]
44+
```
45+
Where the values associated with each key are as follows:
46+
47+
| Key | Value | Example Value shown above|
48+
|:----|:------| :------------------------|
49+
| `fsxId` | The ID of the FSxN file system. | `fs-XXXXXXXXXXXXXXXXX` |
50+
| `secretName` | The name of the secret in Secrets Manager. | `fsxn-credentials` |
51+
| `usernameKey` | The key in the secret that contains the username. | `username` |
52+
| `passwordKey` | The key in the secret that contains the password. | `password` |
53+
54+
:bulb: **NOTE:** If you are going to maintain the `secretsTable` in the source code, and use the
55+
CloudFormation template to deploy the Lambda function, you will have to update the `secretsTable`
56+
variable in the code after the CloudFormation stack is created. Or, edit the source code within
57+
the Cloudformation template itself.
58+
59+
## Deployment
60+
There are two ways to deploy this script. The first way to is use the CloudFormation
61+
template provided in the `cloudformation.yaml` file. The second way is to follow the
62+
steps in the "Manual Setup" section below.
63+
64+
### CloudFormation Deployment
65+
Copy the `cloudformation.yaml` file to your local machine. Then, go to the CloudFormation
66+
service in the AWS console, and click on "Create stack." Select the "Upload a template file"
67+
option and upload the `cloudformation.yaml` file. Click "Next."
68+
69+
On the next page, give the stack a name. Note that this name is used as a suffix to most of the resources it creates
70+
so you might want to keep it short, but meaningful. After the stack name you will need to fill in the following parameters:
71+
72+
| Parameter Name | Description |
73+
|:--------------|:------------|
74+
| subNetIds| List the subnets that you want the Lambda function to run in. They must have connectivity to the FSxN file systems management endpoints. |
75+
| vpcId | The VPC that contains the subnets. This is only used if you are having this CloudFormation template create the AWS service VPC endpoints. |
76+
| securityGroupIds | The security group that the Lambda function will use. This security group should allow access to the AWS service endpoints and the FSx for ONTAP management endpoint over TCP port 443. |
77+
| dynamoDbSecretsTableName | The name of the DynamoDB table that contains the `secretsTable` described above. This value is optional, but if not set, the table commented out in the code will have to be updated to provide the needed information.|
78+
| dynamoDbRegion| The region where the DynamoDB table is located. |
79+
| secretsManagerRegion| The region where the AWS Secrets Manager secrets are located. |
80+
| createWatchdogAlarm | If set to `true` a CloudWatch alarm will be created that will trigger if the Lambda function fails while trying to set the auto size mode on a volume. |
81+
| snsTopicArn| The ARN of the SNS topic that the CloudWatch alarm will send a message to if the Lambda function fails. |
82+
| createSecretManagerEndpoint| If set to `true` a Secrets Manager VPC endpoint will be created. Note that you can only have one VPC service endpoint per service per VPC. |
83+
| createFSxEndpoint| If set to `true` a FSx VPC endpoint will be created. Note that you can only have one VPC service endpoint per service per VPC. |
84+
| createDynamoDbEndpoint| If set to `true` a DynamoDB VPC endpoint will be created. Note that you can only have one VPC service endpoint per service per VPC. |
85+
| routeTableIds| Since the DynamoDB endpoint is a `Gateway` type, routing tables have to be updated to use it. Set this parameter to any route table IDs you want updated. |
86+
| endpointSecurityGroupIds| The security group that the VPC endpoints will use. This security group should allow access to the AWS service the endpoints from the Lambda function over port 443. Since the Lambda function will have the security group specified above assigned to it, it can be used as a network `source` for this security group. |
87+
| autoSizeMode| The auto size mode you want to set the volume to. Valid values are: `grow`, `grow_shrink`, and `off`. |
88+
| growThresholdPrecentage| The percentage of the volume that must be used before a volume will grow. |
89+
| maxGrowSizePercentage| The maximum size the volume can auto grow to expressed in terms of a percentage of the initial volume size. |
90+
| shrinkThresholdPrecentage| The percentage of the volume that must be used before a volume will shrink. |
91+
| minShrinkSizePercentage| The minimum size the volume can auto shrink to expressed in terms of a percentage of the initial volume size. |
92+
| maxWaitTime| The maximum time, in seconds, that the script will wait for the volume to be created before it will give up and exit. This can happen if a lot of volumes are created at the same time. |
93+
94+
Once you have filled in these parameters, click `Next`. On the next page you must accept that this
95+
template can, and does, create roles. Click `Next`. Finally, on the last page, you can review the stack and click `Submit`.
96+
97+
After the stack has been created if you plan to maintain the `secretsTable` within the source code, now would
98+
be the best time to modify it. To do so, go to the Lambda service, find the Lambda function (the name
99+
will start with "auto-set-fsxn-auto-grow" and end with the name you gave the CloudFormation stack)
100+
and use the inline editor to modify the `secretsTable` variable.
101+
102+
To test the function, simply create a volume in the AWS console and check from the ONTAP CLI
103+
that auto size mode appropriately. If it isn't set, check the CloudWatch
104+
logs for the Lambda function to see what went wrong.
105+
106+
:warning: **NOTE:** This program is expecting to be called by a CloudWatch event, if you just click
107+
on the `Test` button within the Lambda console, it will fail since the 'event' structure will not
108+
be set appropriately.
109+
110+
### Manual Setup
111+
If for some reason you can't run the CloudFormation template, here are the steps you can use to manually setup the service:
112+
113+
#### Create secrets in AWS Secrets Manager
15114
Create a secret in Secrets Manager for each of the FSxN file systems you want to manage with
16115
this script. Each secret should have two key value pairs. One that specifies the
17116
user account to use when issuing API calls, and the other that specifies the password for
18117
that account. Note that if you use the same username and password, it is okay
19118
to use the same secret for multiple file systems.
20119

21-
### Create a role for the Lambda function
120+
#### Create a role for the Lambda function
22121
The Lambda function doesn't leverage that many AWS services, so only a few permissions are required:
23122

24-
25123
| Permission | Minimal Scope | Notes
26124
|:------------------------|:----------------|:----------------|
27-
| Allow:logs:CreateLogGroup | arn:aws:logs:<LAMBDA_REGION>:<ACCOUNT_ID>:* | This is required so you can get logs from the Lambda function. |
28-
| Allow:logs:CreateLogStream<BR>Allow:logs:PutLogEvents | arn:aws:logs:<LAMBDA_REGION>:<ACCOUNT_ID>:/aws/lambda/<LAMBDA_FUNCTION_NAME>:* | This is required so you can get logs from the Lambda function. |
29-
| Allow:secretsmanager:GetSecretValue | <ARN_OF_SECRET_WITHIN_SECRETS_MANAGER> | This is required so the Lambda function can get the credentials for the FSxN file system. |
30-
| Allow:dynamodb:Scan | <ARN_OF_DYNAMODB_TABLE> | This is optional, depending on if you put your secretsTable in a DynamoDB. |
31-
| Allow:fsx:DescribeFileSystems<BR>Allow:fsx:DescribeVolumes | * | You can't limit these API. They are required to get information regarding the file system and volumes. |
32-
| Allow:ec2:CreateNetworkInterface<BR>Allow:ec2:DeleteNetworkInterface<BR>Allow:ec2:DescribeNetworkInterfaces | * | Since the Lambda function is going to run within your VPC, it has to be able to create a network interface to communicate with the FSxn file system API. |
33-
34-
### Create AWS Endpoints
35-
Since the Lambda function will be configured to run within the VPC that contains the FSxN
36-
file system, so it can issue API calls against it, there will need to be AWS endpoints so
37-
the Lambda function can access some of the AWS service. If you have a Transit Gateway setup
125+
| Allow:logs:CreateLogGroup | arn:aws:logs:\<LAMBDA_REGION>:\<ACCOUNT_ID>:* | This is required so you can get logs from the Lambda function. |
126+
| Allow:logs:CreateLogStream<BR>Allow:logs:PutLogEvents | arn:aws:logs:\<LAMBDA_REGION>:\<ACCOUNT_ID>:/aws/lambda/\<LAMBDA_FUNCTION_NAME>:* | This is required so you can get logs from the Lambda function. |
127+
| Allow:secretsmanager:GetSecretValue | \<ARNs_OF_SECRETS_WITHIN_SECRETS_MANAGER> | This is required so the Lambda function can get the credentials for the FSxN file system. |
128+
| Allow:dynamodb:Scan | \<ARN_OF_DYNAMODB_TABLE> | This is optional, depending on if you put your `secretsTable` in a DynamoDB table. |
129+
| Allow:fsx:DescribeFileSystems<BR>Allow:fsx:DescribeVolumes | * | You can't limit the scope of these APIs. They are required to get information regarding the file system and volumes. |
130+
| Allow:ec2:CreateNetworkInterface<BR>Allow:ec2:DeleteNetworkInterface<BR>Allow:ec2:DescribeNetworkInterfaces | * | Since the Lambda function is going to run within your VPC, it has to be able to create a network interface to communicate with the FSxN file system endpoints. |
131+
132+
#### Create AWS Endpoints
133+
Since the Lambda function will be configured to run within a VPC that can communicate with the FSxN
134+
file systems, so it can issue API calls against them, there will need to be AWS endpoints so
135+
the Lambda function can also access some of the AWS services. If you have a Transit Gateway setup
38136
that allows access to the Internet, you may not have to create these endpoints, otherwise, the
39-
following endpoints will need to be created, and attached to the VPC and subnets that the
40-
FSxN file system is attached to.
137+
following endpoints will need to be created, and attached to the VPC and subnets that the Lambda
138+
function will run in:
41139

42140
- FSx
43141
- SecretsManager
44-
- DynamoDB - You only need this one if you are going to store you secrtsTable in DynamoDB. It can be a Gateway endpoint.
142+
- DynamoDB - You only need this one if you are going to store your `secretsTable` in DynamoDB. It is recommended that this be a `Gateway` type endpoint. However, if you do that you will also have to update the routing tables associated with the subnets that the Lambda function is deployed on in order for the Lambda function to be able to use it.
143+
144+
:warning: Note that you can only have one service endpoint per service per VPC. So, don't attempt
145+
to add one if one already exists for the VPC you are going to run the Lambda function in.
45146

46-
### Create the Lambda Function
147+
#### Create the Lambda Function
47148
Create a Lambda function with the following parameters:
48149

49150
- Authored from scratch.
50-
- Uses the Python runtime.
151+
- Use the Python runtime.
51152
- Set the permissions to the role created above.
52153
- Enable VPC. Found under the Advanced Settings.
53-
- Attached to the VPC that contains the FSxN file system
54-
- Attached to the Subnets that contain the FSxN file system.
55-
- Attached a security group that allows access from any IP within the two subnets.
154+
- Attached to the VPC that can communicate with the FSxN file systems.
155+
- Attached to the Subnets that can communicate with the FSxN file systems.
156+
- Attached to a security group that allows access from any IP within the two subnets over port 443.
56157

57158
After you create the function, you will be able to insert the code included with this
58159
sample into the code box. Once you have inserted the code, modify the definitions
@@ -63,41 +164,40 @@ is a dictionary with the following keys:
63164
- usernameKey - The name of the key in the secret that contains the username.
64165
- passwordKey - The name of the key in the secret that contains the password.
65166

66-
**NOTE:** Instead of defining the secretsTable in the script, you can define
67-
dynamodbSecretsTableName and dynamodbRegion and the script will read in the
167+
:bulb: **NOTE:** Instead of defining the secretsTable in the code, you can define
168+
dynamoDbSecretsTableName and dynamoDbRegion and the program will read in the
68169
secretsTable information from the specified DynamoDB table. The table should have
69-
the same fields as the secretsTable defined above.
170+
the same fields as the `secretsTable` defined above.
70171

71172
- secretsManagerRegion - Defines the region where your secrets are stored.
72173
- autoSizeMode - Defines the auto size mode you want to set the volume to. Valid values are:
73174
- grow - The volume will automatically grow when it reaches the grow threshold.
74-
- grow_shrink - The volume will automatically grow, and shrink when it reachs the shrink threshold.
175+
- grow_shrink - The volume will automatically grow, and shrink when it reaches the shrink threshold.
75176
- off - The volume will not automatically grow or shrink.
76-
- growThresholdPercentage - The percentage of the volume that must be used before the volume will grow.
77-
- maxGrowSizePercentage - The maximum size the volume can auto grow to expressed in terms of a percentage of the volume size. The default is 200%.
78-
- shrinkThresholdPercentage - The percentage of the volume that must be used before the volume will shrink.
79-
- minShrinkSizePercentage - The minimum size the volume can auto shrink to expressed in terms of a percentage of the volume size. The default is 50%.
177+
- growThresholdPercentage - The percentage of the volume that must be in use before the volume will grow.
178+
- maxGrowSizePercentage - The maximum size the volume can auto grow to, expressed in terms of a percentage of the initial volume size.
179+
- shrinkThresholdPercentage - The percentage of the volume that must be in use before the volume will shrink.
180+
- minShrinkSizePercentage - The minimum size the volume can auto shrink to, expressed in terms of a percentage of the initial volume size.
80181
- maxWaitTime - The maximum time, in seconds, the script will wait for the volume to be created before it will give up and exit.
81182

82-
**NOTE:** Do not delete the variables or set them to None or empty
83-
strings, as the script will not run properly if done so.
183+
:warning: **NOTE:** Do not delete the variables or set them to None or empty strings, as the script will not run properly if done so.
84184

85185
Once you have updated the program, click on the "Deploy" button.
86186

87187
Next, click on the Configuration tab, then General and set the timeout to 2 minutes, or
88-
two times the number of seconds you set the maxWaitTime variable. Note that typically
188+
two times the number of seconds you set the `maxWaitTime` variable. Note that typically
89189
the program will not run this long, but if there are a lot of volumes being created at the
90190
same time, it may have to wait a while for the volume to get created on the ONTAP side before
91191
it can set the auto size mode.
92192

93-
### Create an Event Bridge Rule (a.k.a. CloudWatch Event) that will trigger when a FSx Volume is created
193+
#### Create an Event Bridge Rule (a.k.a. CloudWatch Event) that will trigger when a FSx Volume is created
94194
Once on the "Event Bridge" page, click on Rules on the left-hand side. From there click
95195
on Create Rule. Give the rule a name, and make sure to put the rule on the "Default" bus.
96196
Finally select "Rule with an event pattern" and click Next.
97197

98198
Select "other" as the event source, skip pass the "Sample Event" section, and click on
99-
"Custom pattern (JSON editor)" under the Creation Method. Paste the following in the
100-
Edit Event Pattern text box:
199+
"Custom pattern (JSON editor)" under the Creation Method paste the following in the
200+
`Edit Event Pattern` text box:
101201
```json
102202
{
103203
"detail-type": [
@@ -114,11 +214,14 @@ Edit Event Pattern text box:
114214
}
115215
```
116216

117-
Click Next. This next page will allow you to select the Lambda function you created above.
217+
Click `Next`. The next page will allow you to select the Lambda function you created above.
118218
Just take the defaults for the remaining pages and click on "Create Rule."
119219

120220
At this point every time a volume is created the Lambda function will be called, and it will
121221
attempt to set the auto size mode as specified via the variables at the top of the code.
222+
To confirm it is working, create a volume in the AWS console and check the auto size mode
223+
from the ONTAP CLI. If it isn't set, check the CloudWatch logs for the Lambda function to
224+
see what went wrong.
122225

123226
## Author Information
124227

0 commit comments

Comments
 (0)