Skip to content

Commit 730fedc

Browse files
authored
Merge pull request #125 from NetApp/Auto-Set-FSxN-Auto-Size-Mode
Add a new sample to the repo that will automatically set the auto size mode on a volume when it is created.
2 parents d285706 + 6078b3a commit 730fedc

File tree

2 files changed

+303
-0
lines changed

2 files changed

+303
-0
lines changed
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
# Automatically Set Auto Size mode to Grow on FSx for NetApp ONTAP Volumes
2+
3+
## Introduction
4+
This project helps 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
6+
a Lambda function that will set the mode for you, and instructions on how to set up a
7+
CloudWatch event to trigger the Lambda function whenever a volume is created. With this
8+
combination it ensures that all volumes are effectively created with the auto size mode
9+
set up the way you want for all volumes.
10+
11+
## Set Up
12+
There are just a few things you have to do to set this up:
13+
14+
### Create a role for the Lambda function
15+
The Lambda function doesn't leverage that many AWS services, so only a few permissions are required:
16+
17+
18+
| Permission | Minimal Scope | Notes
19+
|:------------------------|:----------------|:----------------|
20+
| Allow:logs:CreateLogGroup | arn:aws:logs:<LAMBDA_REGION>:<ACCOUNT_ID>:* | This is required so you can get logs from the Lambda function. |
21+
| 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. |
22+
| 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. |
23+
| 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. |
24+
| 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. |
25+
26+
### Create AWS Endpoints
27+
Since the Lambda function will be configured to run within the VPC that contains the FSxN
28+
file system, so it can issue API calls against it, there will need to be AWS endpoints so
29+
the Lambda function can access some of the AWS service. If you have a Transit Gateway setup
30+
that allows access to the Internet, you may not have to create these endpoints, otherwise, the
31+
following endpoints will need to be created, and attached to the VPC and subnets that the
32+
FSxN file system is attached to.
33+
34+
- FSx
35+
- SecretsManager
36+
37+
### Create the Lambda Function
38+
Create a Lambda function with the following parameters:
39+
40+
- Authored from scratch
41+
- Uses the Python runtime
42+
- Set the permissions to the role created above.
43+
- Enable VPC. Found under the Advanced Settings
44+
- Attached to the VPC that contains the FSxN file system
45+
- Attached to the Subnets that contain the FSxN file system.
46+
- Attached a security group that allows access from any IP within the two subnets.
47+
48+
After you create the function, you will be able to insert the code included with this
49+
project into the code box. Once you have inserted the code, modify the "secretsTable"
50+
array to provide the secrets name, and the keys for the username as password for each
51+
of the FSxN File Systems that you want to manage with this script. Also, set the
52+
secretsManagerRegion variable to the region where your secrets are stored. Finally
53+
set the auto size parameters (autoSizeMode, growThresholdPercentage,
54+
maxGrowSizePercentage, shrinkThresholdPercentage, minShrinkSizePercentage and
55+
maxWaitTime) as you see fit. NOTE: Do note delete the variables
56+
or set them to None or empty strings, as the script will fail to run appropriately
57+
if done so.
58+
59+
Once you have updated the program, click on the "Deploy" button.
60+
61+
Next, click on the Configuration tab, then General and set the timeout to 2 minutes, or
62+
two times the number of seconds you set the maxWaitTime variable. Note that typically
63+
the program will not run this long, but if there are a lot of volumes being created at the
64+
same time, it may have to wait a while for the volume to get created on the ONTAP side before
65+
it can set the auto size mode.
66+
67+
### Create an Event Bridge Rule (a.k.a. CloudWatch Event) that will trigger when a FSx Volume is created
68+
Once on the "Event Bridge" page, click on Rules on the left hand side. From there click
69+
on Create Rule. Give the rule a name, and make sure to put the rule on the "Default" bus.
70+
Finally select "Rule with an event pattern" and click Next.
71+
72+
Select "other" as the event source, skip pass the "Sample Event" section, and click on
73+
"Custom pattern (JSON editor)" under the Creation Method. Paste the following in the
74+
Edit Event Pattern text box:
75+
```json
76+
{
77+
"detail-type": [
78+
"AWS API Call via CloudTrail"
79+
],
80+
"detail": {
81+
"eventSource": [
82+
"fsx.amazonaws.com"
83+
],
84+
"eventName": [
85+
"CreateVolume"
86+
]
87+
}
88+
}
89+
```
90+
91+
Click Next. This next page will allow you to select the Lambda function you created above.
92+
Just take the defaults for the remaining pages and click on "Create Rule."
93+
94+
At this point every time a volume is created the Lambda function will be called, and it will
95+
attempt to set the auto size mode as specified via the variables at the top of the code.
96+
97+
## Author Information
98+
99+
This repository is maintained by the contributors listed on [GitHub](https://github.com/NetApp/FSx-ONTAP-samples-scripts/graphs/contributors).
100+
101+
## License
102+
103+
Licensed under the Apache License, Version 2.0 (the "License").
104+
105+
You may obtain a copy of the License at [apache.org/licenses/LICENSE-2.0](http://www.apache.org/licenses/LICENSE-2.0).
106+
107+
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an _"AS IS"_ basis, without WARRANTIES or conditions of any kind, either express or implied.
108+
109+
See the License for the specific language governing permissions and limitations under the License.
110+
111+
© 2024 NetApp, Inc. All Rights Reserved.
Lines changed: 192 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,192 @@
1+
################################################################################
2+
# THIS SOFTWARE IS PROVIDED BY NETAPP "AS IS" AND ANY EXPRESS OR IMPLIED
3+
# WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
4+
# MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
5+
# EVENT SHALL NETAPP BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
6+
# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
7+
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
8+
# OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
9+
# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR'
10+
# OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
11+
# ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
12+
################################################################################
13+
14+
################################################################################
15+
# This Lambda function is used to set the auto size feature to 'grow' on a
16+
# volume that was created in an AWS FSx for NetApp ONTAP file system. It is
17+
# expected to be triggered by a CloudWatch event that is generated when a
18+
# volume is created. The function uses the ONTAP API to set the auto size
19+
# mode to 'grow' on the volume therefore it most run within the VPC where the
20+
# FSx for ONTAP file system is located.
21+
################################################################################
22+
23+
import json
24+
import time
25+
import urllib3
26+
from urllib3.util import Retry
27+
import logging
28+
import boto3
29+
#
30+
# Create a table of secret names and keys for the username and password for each of the FSxIds.
31+
secretsTable = [
32+
{"id": "fs-0e8d9172fa545ef3b", "secretName": "mon-fsxn-credentials", "usernameKey": "mon-fsxn-username", "passwordKey": "mon-fsxn-password"},
33+
{"id": "fs-020de2687bd98ccf7", "secretName": "mon-fsxn-credentials", "usernameKey": "mon-fsxn-username", "passwordKey": "mon-fsxn-password"},
34+
{"id": "fs-07bcb7ad84ac75e43", "secretName": "mon-fsxn-credentials", "usernameKey": "mon-fsxn-username", "passwordKey": "mon-fsxn-password"},
35+
{"id": "fs-077b5ff41951c57b2", "secretName": "mon-fsxn-credentials", "usernameKey": "mon-fsxn-username", "passwordKey": "mon-fsxn-password"}
36+
]
37+
#
38+
# Set the region where the secrets are stored.
39+
secretsManagerRegion="us-west-2"
40+
#
41+
# Set the auto size mode. Supported values are "grow", "grow_shrink", and "off".
42+
autoSizeMode = "grow"
43+
#
44+
# Set the grow-threshold-percentage for the volume. This is the percentage of the volume that must be used before it grows.
45+
growThresholdPercentage = 85
46+
#
47+
# Set the maximum grow size for the volume in terms of the percentage of the provisioned size.
48+
maxGrowSizePercentage = 120
49+
#
50+
# Set the shrink-threshold-percentage for the volume. This is the percentage of the volume that must be free before it shrinks.
51+
shrinkThresholdPercentage = 50
52+
#
53+
# Set the minimum shirtk size for the volume in terms of the percentage of the provisioned size.
54+
minShrinkSizePercentage = 100
55+
#
56+
# Set the iime to wait for a volume to get created. This Lambda function will
57+
# loop waiting for the volume to be created on the ONTAP side so it can set
58+
# the auto size parameters. It will wait up to the number of seconds specified
59+
# below before giving up. NOTE: You must set the timeout of this function
60+
# to at least the number of seconds specified here, and probably two times
61+
# the number here to account for the time it takes to do the API call,
62+
# otherwise the Lambda timeout feature will kill it before it is able to
63+
# iterate as many times as you want it to. Also note that the main reason for
64+
# it to take a while for a volume to get created is when multiple are being
65+
# created at the same time, so if you have automation that might create a lot of
66+
# volumes at the same time, you might need to either adjust this number really
67+
# high, or come up with another way to get the auto size mode.
68+
maxWaitTime=60
69+
70+
################################################################################
71+
# This function is used to obtain the username and password from AWS's Secrets
72+
# Manager for the fsxnId passed in. It returns empty strings if it can't
73+
# find the credentials.
74+
################################################################################
75+
def getCredentials(secretsManagerClient, fsxnId):
76+
77+
for secretItem in secretsTable:
78+
if secretItem['id'] == fsxnId:
79+
secretsInfo = secretsManagerClient.get_secret_value(SecretId=secretItem['secretName'])
80+
secrets = json.loads(secretsInfo['SecretString'])
81+
username = secrets[secretItem['usernameKey']]
82+
password = secrets[secretItem['passwordKey']]
83+
return (username, password)
84+
return ("", "")
85+
86+
################################################################################
87+
# This function returns the AWS structure for a FSxN volume based on the
88+
# volumeId passed it. It confirms that the volume has been created on the ONTAP
89+
# side by checking that the ResourceARN field equals the volumeARN passed in
90+
# that came from the volume creation event and that the UUID field has been
91+
# populated. It returns None if it can't find the volume.
92+
################################################################################
93+
def getVolumeData(fsxClient, volumeId, volumeARN):
94+
95+
global logger
96+
97+
cnt = 0
98+
while cnt < maxWaitTime:
99+
awsVolume = fsxClient.describe_volumes(VolumeIds=[volumeId])['Volumes'][0]
100+
if awsVolume['ResourceARN'] == volumeARN and awsVolume['OntapConfiguration'].get("UUID") != None:
101+
return awsVolume
102+
logger.debug(f'Looping, getting the UUID {cnt}')
103+
cnt += 1
104+
time.sleep(1)
105+
106+
return None
107+
108+
################################################################################
109+
################################################################################
110+
def lambda_handler(event, context):
111+
112+
global logger
113+
#
114+
# Set up "logging" to appropriately display messages. It can be set it up
115+
# to send messages to a syslog server.
116+
logging.basicConfig(datefmt='%Y-%m-%d_%H:%M:%S', format='%(asctime)s:%(name)s:%(levelname)s:%(message)s', encoding='utf-8')
117+
logger = logging.getLogger("set_fsxn_volume_auto_size")
118+
# logger.setLevel(logging.DEBUG)
119+
logger.setLevel(logging.INFO)
120+
#
121+
# Set the logging level higher for these noisy modules to mute thier messages.
122+
logging.getLogger("botocore").setLevel(logging.WARNING)
123+
logging.getLogger("boto3").setLevel(logging.WARNING)
124+
logging.getLogger("urllib3").setLevel(logging.WARNING)
125+
#
126+
# Create a Secrets Manager client.
127+
session = boto3.session.Session()
128+
secretsManagerClient = session.client(service_name='secretsmanager', region_name=secretsManagerRegion)
129+
#
130+
# Disable warning about connecting to servers with self-signed SSL certificates.
131+
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
132+
#
133+
# Set the https retries to 1.
134+
retries = Retry(total=None, connect=1, read=1, redirect=10, status=0, other=0) # pylint: disable=E1123
135+
http = urllib3.PoolManager(cert_reqs='CERT_NONE', retries=retries)
136+
#
137+
# Get the FSxN ID, region, volume name, volume ID, and volume ARN from the CloudWatch event.
138+
fsxId = event['detail']['responseElements']['volume']['fileSystemId']
139+
regionName = event['detail']['awsRegion']
140+
volumeName = event['detail']['requestParameters']['name']
141+
volumeId = event['detail']['responseElements']['volume']['volumeId']
142+
volumeARN = event['detail']['responseElements']['volume']['resourceARN']
143+
if fsxId == "" or regionName == "" or volumeId == "" or volumeName == "" or volumeARN == "":
144+
message = "Couldn't obtain the fsxId, region, volume name, volume ID or volume ARN from the CloudWatch evevnt."
145+
logger.critcal(message)
146+
raise Exception(message)
147+
148+
logger.debug(f'Data from CloudWatch event: FSxID={fsxId}, Region={regionName}, VolumeName={volumeName}, volumeId={volumeId}.')
149+
#
150+
# Get the username and password for the FSxN ID.
151+
(username, password) = getCredentials(secretsManagerClient, fsxId)
152+
if username == "" or password == "":
153+
message = f'No credentials for FSxN ID: {fsxId}.'
154+
logger.critical(message)
155+
raise Exception(message)
156+
#
157+
# Build a header that is used for all the ONTAP API requests.
158+
auth = urllib3.make_headers(basic_auth=f'{username}:{password}')
159+
headers = { **auth }
160+
#
161+
# Get the management IP of the FSxN file system.
162+
fsxClient = boto3.client('fsx', region_name = regionName)
163+
fs = fsxClient.describe_file_systems(FileSystemIds = [fsxId])['FileSystems'][0]
164+
fsxnIp = fs['OntapConfiguration']['Endpoints']['Management']['IpAddresses'][0]
165+
if fsxnIp == "":
166+
message = f"Can't find management IP for FSxN file system with an ID of '{fsxId}'."
167+
logger.critical(message)
168+
raise Exception(message)
169+
#
170+
# Get the volume UUID and volume size based on the volume ID.
171+
volumeData = getVolumeData(fsxClient, volumeId, volumeARN)
172+
if volumeData == None:
173+
message=f'Failed to get volume information for volumeID: {volumeId}.'
174+
logger.critical(message)
175+
raise Exception(message)
176+
volumeUUID = volumeData["OntapConfiguration"]["UUID"]
177+
volumeSizeInMegabytes = volumeData["OntapConfiguration"]["SizeInMegabytes"]
178+
#
179+
# Set the auto grow feature.
180+
try:
181+
endpoint = f'https://{fsxnIp}/api/storage/volumes/{volumeUUID}'
182+
maximum = volumeSizeInMegabytes * maxGrowSizePercentage / 100 * 1024 * 1024
183+
minimum = volumeSizeInMegabytes * minShrinkSizePercentage / 100 * 1024 * 1024
184+
data = json.dumps({"autosize": {"mode": autoSizeMode, "grow_threshold": growThresholdPercentage, "maximum": maximum, "minimum": minimum, "shrink_threshold": shrinkThresholdPercentage}})
185+
logger.debug(f'Trying {endpoint} with {data}.')
186+
response = http.request('PATCH', endpoint, headers=headers, timeout=5.0, body=data)
187+
if response.status >= 200 and response.status <= 299:
188+
logger.info(f"Updated the auto size parameters for volume name {volumeName}.")
189+
else:
190+
logger.error(f'API call to {endpoint} failed. HTTP status code: {response.status}. Error message: {response.data}.')
191+
except Exception as err:
192+
logger.critical(f'Failed to issue API against {fsxnIp}. The error messages received: "{err}".')

0 commit comments

Comments
 (0)