From 480679a4c5956e7618e7dcd8d5e58b3ab0a0b5c5 Mon Sep 17 00:00:00 2001 From: Sergey Novikov Date: Wed, 30 Oct 2024 16:56:47 +0100 Subject: [PATCH] OPS-6304 Add basic configuration --- Makefile | 2 +- README.md | 124 +++++++++++++++++++++++++++++++++---- data.tf | 3 + examples/titan/README.md | 36 +++++++++++ examples/titan/main.tf | 28 +++++++++ examples/titan/versions.tf | 9 +++ main.tf | 37 +++++++++++ outputs.tf | 9 +++ variables.tf | 69 +++++++++++++++++++++ versions.tf | 6 ++ 10 files changed, 309 insertions(+), 14 deletions(-) create mode 100644 data.tf create mode 100644 examples/titan/README.md create mode 100644 examples/titan/main.tf create mode 100644 examples/titan/versions.tf create mode 100644 main.tf create mode 100644 outputs.tf create mode 100644 variables.tf diff --git a/Makefile b/Makefile index affb4f3..15f1d75 100644 --- a/Makefile +++ b/Makefile @@ -77,7 +77,7 @@ test: _pull-tf echo "------------------------------------------------------------"; \ echo "# Terraform init"; \ echo "------------------------------------------------------------"; \ - if docker run $$(tty -s && echo "-it" || echo) --rm -v "$(CURRENT_DIR):/t" --workdir "$${DOCKER_PATH}" hashicorp/terraform:$(TF_VERSION) \ + if docker run $$(tty -s && echo "-it" || echo) --rm --network host -v "$(CURRENT_DIR):/t" --workdir "$${DOCKER_PATH}" hashicorp/terraform:$(TF_VERSION) \ init \ -lock=false \ -upgrade \ diff --git a/README.md b/README.md index da64134..11fc28a 100644 --- a/README.md +++ b/README.md @@ -1,11 +1,10 @@ -# terraform-module-template -Template for Terraform modules - - +# terraform-aws-bedrock + +Module to create Amazon Bedrock custom model + +[![lint](https://github.com/flaconi/terraform-aws-bedrock/workflows/lint/badge.svg)](https://github.com/flaconi/terraform-aws-bedrock/actions?query=workflow%3Alint) +[![test](https://github.com/flaconi/terraform-aws-bedrock/workflows/test/badge.svg)](https://github.com/flaconi/terraform-aws-bedrock/actions?query=workflow%3Atest) +[![Tag](https://img.shields.io/github/tag/flaconi/terraform-aws-bedrock.svg)](https://github.com/flaconi/terraform-aws-bedrock/releases) [![License](https://img.shields.io/badge/license-MIT-blue.svg)](https://opensource.org/licenses/MIT) For requirements regarding module structure: [style-guide-terraform.md](https://github.com/Flaconi/devops-docs/blob/master/doc/conventions/style-guide-terraform.md) @@ -18,7 +17,9 @@ For requirements regarding module structure: [style-guide-terraform.md](https:// ## Providers -No providers. +| Name | Version | +|------|---------| +| [aws](#provider\_aws) | ~> 5.73 | @@ -28,24 +29,121 @@ No providers. | Name | Version | |------|---------| | [terraform](#requirement\_terraform) | ~> 1.3 | +| [aws](#requirement\_aws) | ~> 5.73 | ## Required Inputs -No required inputs. +The following input variables are required: + +### [name](#input\_name) + +Description: Name for the custom model. + +Type: `string` + +### [job\_name](#input\_job\_name) + +Description: A name for the customization job. + +Type: `string` + +### [model\_role\_arn](#input\_model\_role\_arn) + +Description: The Amazon Resource Name (ARN) of an IAM role that Bedrock can assume to perform tasks on your behalf. + +Type: `string` + +### [output\_data\_s3\_uri](#input\_output\_data\_s3\_uri) + +Description: The S3 URI where the output data is stored. + +Type: `string` + +### [training\_data\_s3\_uri](#input\_training\_data\_s3\_uri) + +Description: The S3 URI where the training data is stored. + +Type: `string` ## Optional Inputs -No optional inputs. +The following input variables are optional (have default values): + +### [foundation\_model\_id](#input\_foundation\_model\_id) + +Description: Model identifier. + +Type: `string` + +Default: `"amazon.titan-text-express-v1"` + +### [model\_kms\_key\_id](#input\_model\_kms\_key\_id) + +Description: The custom model is encrypted at rest using this key. Specify the key ARN. + +Type: `string` + +Default: `null` + +### [model\_customization\_type](#input\_model\_customization\_type) + +Description: The customization type. Valid values: `FINE_TUNING`, `CONTINUED_PRE_TRAINING`. + +Type: `string` + +Default: `null` + +### [model\_hyperparameters](#input\_model\_hyperparameters) + +Description: Parameters related to tuning the model. + +Type: `map(string)` + +Default: `{}` + +### [validation\_data\_s3\_uri](#input\_validation\_data\_s3\_uri) + +Description: The S3 URI where the validation data is stored. + +Type: `string` + +Default: `null` + +### [vpc\_config](#input\_vpc\_config) + +Description: Configuration parameters for the private Virtual Private Cloud (VPC) that contains the resources you are using for this job. + +Type: + +```hcl +object({ + security_group_ids = list(string) + subnet_ids = list(string) + }) +``` + +Default: `null` + +### [tags](#input\_tags) + +Description: A map of tags to assign to the customization job and custom model. + +Type: `map(string)` + +Default: `{}` ## Outputs -No outputs. +| Name | Description | +|------|-------------| +| [custom\_model\_arn](#output\_custom\_model\_arn) | The ARN of the output model. | +| [job\_arn](#output\_job\_arn) | The ARN of the customization job. | @@ -53,4 +151,4 @@ No outputs. **[MIT License](LICENSE)** -Copyright (c) 2023 **[Flaconi GmbH](https://github.com/flaconi)** +Copyright (c) 2024 **[Flaconi GmbH](https://github.com/flaconi)** diff --git a/data.tf b/data.tf new file mode 100644 index 0000000..02dcde6 --- /dev/null +++ b/data.tf @@ -0,0 +1,3 @@ +data "aws_bedrock_foundation_model" "this" { + model_id = var.foundation_model_id +} diff --git a/examples/titan/README.md b/examples/titan/README.md new file mode 100644 index 0000000..0badb4b --- /dev/null +++ b/examples/titan/README.md @@ -0,0 +1,36 @@ +# Example + + +## Requirements + +| Name | Version | +|------|---------| +| [terraform](#requirement\_terraform) | >= 1.3 | +| [aws](#requirement\_aws) | ~> 5.73 | + +## Providers + +No providers. + +## Modules + +| Name | Source | Version | +|------|--------|---------| +| [model](#module\_model) | ../../ | n/a | + +## Resources + +No resources. + +## Inputs + +No inputs. + +## Outputs + +| Name | Description | +|------|-------------| +| [custom\_model\_arn](#output\_custom\_model\_arn) | The ARN of the output model. | +| [job\_arn](#output\_job\_arn) | The ARN of the customization job. | + + diff --git a/examples/titan/main.tf b/examples/titan/main.tf new file mode 100644 index 0000000..7e08c28 --- /dev/null +++ b/examples/titan/main.tf @@ -0,0 +1,28 @@ +module "model" { + source = "../../" + + name = "titan" + job_name = "titan-job-1" + foundation_model_id = "amazon.titan-text-express-v1" + model_role_arn = "arn:aws:iam::123456789101:role/TitanModelRole" + + model_hyperparameters = { + "epochCount" = "1" + "batchSize" = "1" + "learningRate" = "0.005" + "learningRateWarmupSteps" = "0" + } + + output_data_s3_uri = "s3://titan-output-data/data/" + training_data_s3_uri = "s3://titan-training-data/data/train.jsonl" +} + +output "custom_model_arn" { + description = "The ARN of the output model." + value = module.model.custom_model_arn +} + +output "job_arn" { + description = "The ARN of the customization job." + value = module.model.job_arn +} diff --git a/examples/titan/versions.tf b/examples/titan/versions.tf new file mode 100644 index 0000000..74007d0 --- /dev/null +++ b/examples/titan/versions.tf @@ -0,0 +1,9 @@ +terraform { + required_version = ">= 1.3" + required_providers { + aws = { + source = "hashicorp/aws" + version = "~> 5.73" + } + } +} diff --git a/main.tf b/main.tf new file mode 100644 index 0000000..7bf9f49 --- /dev/null +++ b/main.tf @@ -0,0 +1,37 @@ +resource "aws_bedrock_custom_model" "this" { + custom_model_name = var.name + job_name = var.job_name + + base_model_identifier = data.aws_bedrock_foundation_model.this.model_arn + custom_model_kms_key_id = var.model_kms_key_id + customization_type = var.model_customization_type + hyperparameters = var.model_hyperparameters + role_arn = var.model_role_arn + + output_data_config { + s3_uri = var.output_data_s3_uri + } + + training_data_config { + s3_uri = var.training_data_s3_uri + } + + dynamic "validation_data_config" { + for_each = var.training_data_s3_uri != null ? ["this"] : [] + content { + validator { + s3_uri = var.validation_data_s3_uri + } + } + } + + dynamic "vpc_config" { + for_each = var.vpc_config != null ? ["this"] : [] + content { + security_group_ids = var.vpc_config["security_group_ids"] + subnet_ids = var.vpc_config["subnet_ids"] + } + } + + tags = var.tags +} diff --git a/outputs.tf b/outputs.tf new file mode 100644 index 0000000..735f8a9 --- /dev/null +++ b/outputs.tf @@ -0,0 +1,9 @@ +output "custom_model_arn" { + description = "The ARN of the output model." + value = aws_bedrock_custom_model.this.custom_model_arn +} + +output "job_arn" { + description = "The ARN of the customization job." + value = aws_bedrock_custom_model.this.job_arn +} diff --git a/variables.tf b/variables.tf new file mode 100644 index 0000000..69eb882 --- /dev/null +++ b/variables.tf @@ -0,0 +1,69 @@ +variable "name" { + description = "Name for the custom model." + type = string +} + +variable "job_name" { + description = "A name for the customization job." + type = string +} + +variable "foundation_model_id" { + description = "Model identifier." + type = string + default = "amazon.titan-text-express-v1" +} + +variable "model_kms_key_id" { + description = "The custom model is encrypted at rest using this key. Specify the key ARN." + type = string + default = null +} + +variable "model_customization_type" { + description = "The customization type. Valid values: `FINE_TUNING`, `CONTINUED_PRE_TRAINING`." + type = string + default = null +} + +variable "model_hyperparameters" { + description = "Parameters related to tuning the model." + type = map(string) + default = {} +} + +variable "model_role_arn" { + description = "The Amazon Resource Name (ARN) of an IAM role that Bedrock can assume to perform tasks on your behalf." + type = string +} + +variable "output_data_s3_uri" { + description = "The S3 URI where the output data is stored." + type = string +} + +variable "training_data_s3_uri" { + description = "The S3 URI where the training data is stored." + type = string +} + +variable "validation_data_s3_uri" { + description = "The S3 URI where the validation data is stored." + type = string + default = null +} + +variable "vpc_config" { + description = "Configuration parameters for the private Virtual Private Cloud (VPC) that contains the resources you are using for this job." + type = object({ + security_group_ids = list(string) + subnet_ids = list(string) + }) + default = null +} + +variable "tags" { + description = "A map of tags to assign to the customization job and custom model." + type = map(string) + default = {} +} diff --git a/versions.tf b/versions.tf index e6b4cbd..c06253b 100644 --- a/versions.tf +++ b/versions.tf @@ -1,3 +1,9 @@ terraform { required_version = "~> 1.3" + required_providers { + aws = { + source = "hashicorp/aws" + version = "~> 5.73" + } + } }