AWS Lambda Functions and Knowledge Base used in Account factory for Terraform with Amazon Bedrock Agents
This README documents two AWS Lambda functions designed for automating AWS account setup and customization using Amazon Bedrock and Account Factory for Terraform (AFT), along with a Knowledge Base (KB) used by these Lambdas. This solution follows AWS security reference architecture(https://docs.aws.amazon.com/prescriptive-guidance/latest/security-reference-architecture/architecture.html), and can be utilized to create AWS account types such as security tooling, infrastructure, workload accounts, and deploy respective AWS services for each type of AWS account. This blog focuses on creating the security tooling account and deployment of the recommended AWS security services.
- The user utilizes the bedrock agent chat console to input their AWS account creation requirements. For instance, the user might specify, "Create an AWS Account for Security Tooling". The agent is configured with instructions and bedrock knowledge base to customize and create an AWS account via Account Factory for Terraform.
- On receiving the above example request, the agent queries a Bedrock Knowledge Base predefined with recommended AWS security services for Security Tooling AWS account. The agent presents these security services to the user. The user inputs a subset or all of the recommended security services.
- Next, the agent collects user information for account creation. Based on the AWS services the user selects, the agent passes the service information to an action group that invokes the Lambda function(Account customization lambda). This function retrieves approved Terraform module configurations from a predefined Knowledge Base, uses bedrock LLM model to construct modular terraform code, and pushes the terraform code to AFT account customization repository(learn-terraform-aft-account-customizations).
- Before moving to the next step for account creation with the user selected AWS security services, the agent asks the user to confirm/update terraform code that was published to the AFT account customization repo.
- Once the agent receives user confirmation, the agent passes the the information to another action group that invokes a lambda function(Account creation Lambda) that publishes the AWS account creation terraform module to AFT account request repository (learn-terraform-aft-account-request) and AWS Control Tower Account Factory for Terraform (AFT) pipeline is triggered.
Solution Workflow:
The below diagram pictorially represents how the solution works.
See an example illustrative chat, showcasing how users can interact with Bedrock agent to seamlessly create a Security-tooling account:
- Description: Automates AWS account creation by appending new Terraform modules to a GitHub repository.
- Environment Variables:
GITHUB_TOKEN
: Token for GitHub API authentication.KNOWLEDGE_BASE_ID
: ID of created Knowledge base
- Dependencies: Python 3.x,
requests
library. - Logical Flow:
- Receives an event with account details.
- Extracts parameters and constructs a Terraform module snippet.
- Checks GitHub repository for existing
main.tf
file. - Appends new Terraform module to existing content.
- Encodes updated content and commits to GitHub.
- Returns success or error message.
- Description: Generates and commits Terraform configurations for custom AWS services to a GitHub repository.
- Environment Variables:
GITHUB_TOKEN
: Token for GitHub API authentication.
- Dependencies: Python 3.x,
boto3
,requests
,logging
,base64
libraries. - Logical Flow:
- Receives an event with customization details.
- Splits and processes AWS service requests.
- Retrieves Terraform module definitions from the KB.
- Invokes Bedrock model to generate Terraform and README contents.
- Commits generated Terraform configuration and README to GitHub.
- Returns success message with GitHub URLs or error information.
- Provide a name for each of your action group and describe what the action does in the Description for action groups.
- In Select Lambda function, for both your action groups choose the appropriate Lambda functions that you created in AWS Lambda. The Lambda function provides the business logic that is carried out upon invoking the action. Choose the version of the function to use. For more information, see Action group Lambda functions
- In Select API schema, provide a link to the Amazon S3 URI of the schema with the API description, structure, and parameters for the action group. APIs manages the logic for receiving user inputs and triggering the Lambda functions for account creation and customization. The API should be designed to handle various tasks, such as validating user inputs, initiating the Terraform module creation process, and monitoring the status of account provisioning. For more information, see Action group OpenAPI schemas.
- Select Add another Action group to set up another action group for your agent. When you are done adding action groups, select Next.
- Description: A structured repository containing AWS service and Terraform module information.
- Structure: JSON format categorizing services and modules.
- Configure Knowledge Base: Configuring a Knowledge Base (KB) enables your Bedrock agents to access a repository of information for AWS account provisioning. Follow these steps to set up your KB:
- Access the Amazon Bedrock Console: Log in and go directly to the 'Knowledge Base' section. This is your starting point for creating a new KB.
- Name Your Knowledge Base: Choose a clear and descriptive name that reflects the purpose of your KB, such as "AWS Account Setup KB."
- Select an IAM Role: Assign a pre-configured IAM role with the necessary permissions.
- Define the Data Source: Upload a JSON file to an S3 bucket with encryption enabled for security. This file should contain a structured list of AWS services and Terraform modules. For the JSON structure, use the example provided in this repository
- Choose the Default Embeddings Model: For most use cases, the Amazon Bedrock Titan G1 Embeddings - Text model will suffice. It's pre-configured and ready to use, simplifying the process.
- Opt for the Managed Vector Store: Allow Amazon Bedrock to create and manage the vector store for you in Amazon OpenSearch Service.
- Review and Finalize: Double-check all entered information for accuracy. Pay special attention to the S3 bucket URI and IAM role details.
- Lambda Functions:
- Regularly update dependencies and environment variables.
- Monitor Lambda logs for troubleshooting.
- Knowledge Base:
- Regularly update with new AWS services and modules.
- Validate JSON structure after updates.
See CONTRIBUTING for more information.
This library is licensed under the MIT-0 License. See the LICENSE file.