Skip to content

Commit d1cb9e7

Browse files
authored
V0.0.4 (#11)
* better parser for the output * refine prompt for the plan summary
1 parent 72f4543 commit d1cb9e7

File tree

5 files changed

+101
-43
lines changed

5 files changed

+101
-43
lines changed

.header.md

Lines changed: 41 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,42 +2,64 @@
22

33
## Overview
44

5-
Integrate Amazon Bedrock to your HashiCorp Cloud Platform Terraform (Terraform Cloud) Run Tasks for:
5+
Enhance your HashiCorp Cloud Platform Terraform (Terraform Cloud) workflows with AI-powered analysis using Amazon Bedrock. This module integrates seamlessly as a Run Task to provide:
66

7-
* Analyzing Terraform plan and generate short-summary
7+
* Intelligent Terraform plan analysis with concise, human-friendly summaries
8+
* Advanced function calling capabilities for extended analysis (e.g., AMI evaluation)
9+
* Responsible AI implementation with customizable guardrails
810

9-
* Function calling for other API-based analysis (e.g AMI analysis)
11+
### Key Features
1012

11-
### Terraform plan summary
13+
1. **AI-Powered Plan Summaries**
14+
* Generate clear, natural language summaries of Terraform plan outputs
15+
* Quickly understand the impact of infrastructure changes
16+
![Example](./images/example.png)
1217

13-
Summarize Terraform plan output in human friendly natural language.
14-
![Example](./images/example.png)
18+
2. **Extensible Analysis with Function Calling**
19+
* Leverage AI to perform additional analyses, such as AMI evaluation
20+
* Easily extend to other API-based tools and services
21+
![Example2](./images/example2.png)
1522

16-
### Function calling (AMI analysis)
23+
3. **Responsible AI Integration**
24+
* Implement Amazon Bedrock guardrails tailored to your organization's policies
25+
* Ensure ethical and compliant AI usage in your infrastructure workflows
26+
![Example3](./images/example3.png)
1727

18-
Use function calling to execute other tools, such as analyzing AMI data.
19-
![Example2](./images/example2.png)
28+
4. **Secure Architecture**
29+
* Designed for deployment in a dedicated AWS account
30+
* Optional AWS WAF integration for enhanced endpoint protection
31+
* Adherence to AWS security best practices
2032

21-
### Responsible AI
33+
5. **Seamless Terraform Cloud Integration**
34+
* Operates as a native Run Task within your Terraform Cloud workflow
35+
* Provides insights without disrupting existing processes
2236

23-
Implement safeguards using Amazon Bedrock guardrails customized to your organization requirements and responsible AI policies
24-
![Example3](./images/example3.png)
37+
6. **Flexible and Customizable**
38+
* Adapt the module to fit your specific organizational needs
39+
* Easily configure and extend guardrails and analysis capabilities
2540

2641
## Architecture
2742

2843
![Diagram](./images/arch.png)
2944

30-
This module must be deployed in AWS account with access to Amazon Bedrock model. We recommend you to use hub-spoke model, by deploying this module in one dedicated account.
31-
32-
Please refer to the [best-practice](#best-practice) section below for more details.
45+
This module leverages a hub-spoke model, designed for deployment in a dedicated AWS account with Amazon Bedrock access. It utilizes AWS Lambda, CloudFront, and other AWS services to provide a scalable and secure solution.
3346

3447
## Prerequisites
3548

36-
To use this module you need have the following:
49+
To implement this module, you'll need:
50+
51+
1. An AWS account with appropriate credentials
52+
2. Access to Amazon Bedrock (default model: Claude 3 Sonnet)
53+
3. A HashiCorp Cloud Platform (HCP) Terraform account
54+
55+
## Getting Started
56+
57+
For detailed setup instructions and best practices, please refer to the sections below:
58+
59+
* [Usage](#usage)
60+
* [Best Practices](#best-practice)
3761

38-
1. AWS account and credentials
39-
1. Amazon Bedrock model access (default model is `Claude 3 Sonnet`)
40-
1. HCP Terraform account
62+
Enhance your Terraform workflows with AI-powered insights while maintaining security and responsible AI practices.
4163

4264
## Usage
4365

README.md

Lines changed: 41 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,42 +3,64 @@
33

44
## Overview
55

6-
Integrate Amazon Bedrock to your HashiCorp Cloud Platform Terraform (Terraform Cloud) Run Tasks for:
6+
Enhance your HashiCorp Cloud Platform Terraform (Terraform Cloud) workflows with AI-powered analysis using Amazon Bedrock. This module integrates seamlessly as a Run Task to provide:
77

8-
* Analyzing Terraform plan and generate short-summary
8+
* Intelligent Terraform plan analysis with concise, human-friendly summaries
9+
* Advanced function calling capabilities for extended analysis (e.g., AMI evaluation)
10+
* Responsible AI implementation with customizable guardrails
911

10-
* Function calling for other API-based analysis (e.g AMI analysis)
12+
### Key Features
1113

12-
### Terraform plan summary
14+
1. **AI-Powered Plan Summaries**
15+
* Generate clear, natural language summaries of Terraform plan outputs
16+
* Quickly understand the impact of infrastructure changes
17+
![Example](./images/example.png)
1318

14-
Summarize Terraform plan output in human friendly natural language.
15-
![Example](./images/example.png)
19+
2. **Extensible Analysis with Function Calling**
20+
* Leverage AI to perform additional analyses, such as AMI evaluation
21+
* Easily extend to other API-based tools and services
22+
![Example2](./images/example2.png)
1623

17-
### Function calling (AMI analysis)
24+
3. **Responsible AI Integration**
25+
* Implement Amazon Bedrock guardrails tailored to your organization's policies
26+
* Ensure ethical and compliant AI usage in your infrastructure workflows
27+
![Example3](./images/example3.png)
1828

19-
Use function calling to execute other tools, such as analyzing AMI data.
20-
![Example2](./images/example2.png)
29+
4. **Secure Architecture**
30+
* Designed for deployment in a dedicated AWS account
31+
* Optional AWS WAF integration for enhanced endpoint protection
32+
* Adherence to AWS security best practices
2133

22-
### Responsible AI
34+
5. **Seamless Terraform Cloud Integration**
35+
* Operates as a native Run Task within your Terraform Cloud workflow
36+
* Provides insights without disrupting existing processes
2337

24-
Implement safeguards using Amazon Bedrock guardrails customized to your organization requirements and responsible AI policies
25-
![Example3](./images/example3.png)
38+
6. **Flexible and Customizable**
39+
* Adapt the module to fit your specific organizational needs
40+
* Easily configure and extend guardrails and analysis capabilities
2641

2742
## Architecture
2843

2944
![Diagram](./images/arch.png)
3045

31-
This module must be deployed in AWS account with access to Amazon Bedrock model. We recommend you to use hub-spoke model, by deploying this module in one dedicated account.
32-
33-
Please refer to the [best-practice](#best-practice) section below for more details.
46+
This module leverages a hub-spoke model, designed for deployment in a dedicated AWS account with Amazon Bedrock access. It utilizes AWS Lambda, CloudFront, and other AWS services to provide a scalable and secure solution.
3447

3548
## Prerequisites
3649

37-
To use this module you need have the following:
50+
To implement this module, you'll need:
51+
52+
1. An AWS account with appropriate credentials
53+
2. Access to Amazon Bedrock (default model: Claude 3 Sonnet)
54+
3. A HashiCorp Cloud Platform (HCP) Terraform account
55+
56+
## Getting Started
57+
58+
For detailed setup instructions and best practices, please refer to the sections below:
59+
60+
* [Usage](#usage)
61+
* [Best Practices](#best-practice)
3862

39-
1. AWS account and credentials
40-
1. Amazon Bedrock model access (default model is `Claude 3 Sonnet`)
41-
1. HCP Terraform account
63+
Enhance your Terraform workflows with AI-powered insights while maintaining security and responsible AI practices.
4264

4365
## Usage
4466

VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
v0.0.3
1+
v0.0.4

images/example.png

46.9 KB
Loading

lambda/runtask_fulfillment/ai.py

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from runtask_utils import generate_runtask_result
99
from tools.get_ami_releases import GetECSAmisReleases
1010
from utils import logger, stream_messages, tool_config
11+
import xml.etree.ElementTree as ET
1112

1213
# Initialize model_id and region
1314
model_id = os.environ.get("BEDROCK_LLM_MODEL")
@@ -180,15 +181,27 @@ def eval(tf_plan_json):
180181
# Add response to message history
181182
messages.append(response)
182183

183-
result = response["content"][0]["text"]
184+
# Try to parse output as XML and look for the <output> tag
185+
try:
186+
root = ET.fromstring(response["content"][0]["text"])
187+
result = root.find("result").text
188+
logger.info("Parsed : {}".format(result))
189+
except Exception as e:
190+
result = response["content"][0]["text"]
191+
logger.info("Non Parsed : {}".format(result))
184192

185193
#####################################################################
186194
######### Third, generate short summary #########
187195
#####################################################################
188196

189197
logger.info("##### Generating short summary #####")
190198
prompt = f"""
191-
Can you provide a short summary with maximum of 150 characters of the infrastructure changes?
199+
List the resources that will be created, modified or deleted in the following terraform plan using the following rules:
200+
- Provide summary of the infrastructure changes
201+
- Highlight major components of the changes such as what Terraform modules is executed
202+
- Summarize what each Terraform modules does
203+
- Highlight any resources that being replaced or deleted
204+
- Highlight any outputs if available
192205
193206
<terraform_plan>
194207
{tf_plan_json["resource_changes"]}
@@ -214,7 +227,7 @@ def eval(tf_plan_json):
214227

215228
guardrail_status, guardrail_response = guardrail_inspection(str(description))
216229
if guardrail_status:
217-
results.append(generate_runtask_result(outcome_id="Plan-Summary", description="Summary of Terraform plan", result=description[:700]))
230+
results.append(generate_runtask_result(outcome_id="Plan-Summary", description="Summary of Terraform plan", result=description[:9000])) # body max limit of 10,000 chars
218231
else:
219232
results.append(generate_runtask_result(outcome_id="Plan-Summary", description="Summary of Terraform plan", result="Output omitted due to : {}".format(guardrail_response)))
220233
description = "Bedrock guardrail triggered : {}".format(guardrail_response)
@@ -225,7 +238,8 @@ def eval(tf_plan_json):
225238
else:
226239
results.append(generate_runtask_result(outcome_id="AMI-Summary", description="Summary of AMI changes", result="Output omitted due to : {}".format(guardrail_response)))
227240

228-
return description, results
241+
runtask_high_level ="Terraform plan analyzer using Amazon Bedrock, expand the findings below to learn more. Click `view more details` to get the detailed logs"
242+
return runtask_high_level, results
229243

230244
def guardrail_inspection(input_text, input_mode = 'OUTPUT'):
231245

0 commit comments

Comments
 (0)