Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Configure terraform api #139

Merged
merged 41 commits into from
Sep 12, 2024
Merged

Configure terraform api #139

merged 41 commits into from
Sep 12, 2024

Conversation

italopessoa
Copy link
Member

No description provided.

Copy link

coderabbitai bot commented Sep 12, 2024

Warning

Rate limit exceeded

@italopessoa has exceeded the limit for the number of commits or files that can be reviewed per hour. Please wait 11 minutes and 47 seconds before requesting another review.

How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

Commits

Files that changed from the base of the PR and between 57b0677 and a92657f.

Walkthrough

The changes introduced in this pull request enhance the CI/CD pipeline by integrating Terraform workflows for automated infrastructure management. New environment variables are added to the GitHub Actions workflows, and a new workflow for executing Terraform plans is created. The modifications include a new job for generating configuration files and updating pull requests with plan outputs, facilitating better management of infrastructure based on Docker images.

Changes

Files Change Summary
.github/workflows/dotnet.yml Added environment variables for Terraform configuration, modified job dependencies, defined output variable API_IMAGE_TAG, and introduced a new job create-app to generate a .auto.tfvars file and manage infrastructure creation.
.github/workflows/terraform-plan.yaml Created a new workflow for executing Terraform plans on pull requests, including steps for checking out code, creating a .auto.tfvars file, uploading configuration to Terraform Cloud, creating a plan run, and updating the pull request with plan output.
tf/variables.tf Introduced a new file with variable declarations for AWS infrastructure configuration, including sensitive information handling and essential parameters for RDS, EKS, and API management.

Assessment against linked issues

Objective Addressed Explanation
Integrate Terraform workflows into CI/CD (1)
Automate Terraform plan execution on PRs (2)
Manage infrastructure based on Docker images (3)

Possibly related issues


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

Share
Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai or @coderabbitai title anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 3

Outside diff range and nitpick comments (2)
tf/providers.tf (1)

24-27: Remove the commented out code.

The commented out code for configuring the Kubernetes provider using a local kubeconfig file is no longer needed as the Kubernetes provider is configured to use the EKS cluster in the next code segment. Consider removing the commented out code to keep the configuration clean and maintainable.

Apply this diff to remove the commented out code:

-# provider "kubernetes" {
-#   config_path    = "C:\\Users\\italo\\.kube\\config"
-#   config_context = "minikube"
-# }
tf/main.tf (1)

227-319: LGTM with a suggestion!

The Kubernetes Service and Deployment for Seq are correctly defined. The configurations look good and follow the best practices:

  • The Service uses NLB with an internal scheme for load balancing.
  • The Deployment specifies the required configurations, such as replica count, container image, ports, resources, and environment variables.

However, I noticed that the host path volume for storing dashboards is commented out and not being used.

If it is not required, consider removing the commented out code to keep the configuration clean and maintainable:

- # volume {
- #   name = "seq-persistent-storage"
- #   persistent_volume_claim {
- #     claim_name = "pvc-seq"
- #   }
- # }
- volume {
-   name = "dashboards-volume"
-   host_path {
-     path = "/home/docker/seq"
-   }
- }
Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between b969ea3 and 752ca21.

Files selected for processing (6)
  • .github/workflows/dotnet.yml (3 hunks)
  • .github/workflows/terraform-plan.yaml (1 hunks)
  • tf/main.tf (1 hunks)
  • tf/output.tf (1 hunks)
  • tf/providers.tf (1 hunks)
  • tf/variables.tf (1 hunks)
Additional context used
actionlint
.github/workflows/terraform-plan.yaml

27-27: property "build-docker-image" is not defined in object type {}

(expression)

Additional comments not posted (34)
tf/output.tf (1)

1-4: LGTM!

The output variable is correctly defined, and setting the sensitive attribute to true is a good practice for handling sensitive information like cluster details.

tf/providers.tf (3)

1-9: LGTM!

Pinning the AWS provider and Terraform versions is a good practice to ensure consistent behavior across different environments. The versions used are recent and should be compatible with the latest features and bug fixes.


11-21: LGTM!

The AWS provider configuration looks good:

  • Using variables for the profile and region allows for flexibility and reusability across different environments.
  • Aliasing the provider is useful when multiple providers with different configurations are needed.
  • Setting default tags is a good practice for resource management and tracking.

29-38: LGTM!

The Kubernetes provider configuration for EKS looks good:

  • Using the EKS cluster endpoint and certificate ensures that the Kubernetes provider can communicate with the EKS cluster securely.
  • Retrieving the EKS cluster data using the aws_eks_cluster data source is a good practice as it ensures that the configuration is always up to date.
  • Using the exec block to authenticate with the EKS cluster using the aws CLI is a secure and recommended approach.
tf/variables.tf (12)

1-4: LGTM!

The variable is correctly defined with a default value.


6-10: LGTM!

The variable is correctly defined with a default value.


12-16: LGTM!

The variable is correctly defined with a default value.


18-21: LGTM!

The variable is correctly defined with a default value.


23-26: LGTM!

The variable is correctly defined with a default value.


28-31: LGTM!

The variable is correctly defined as a sensitive string.


33-36: LGTM!

The variable is correctly defined as a sensitive string.


38-41: LGTM!

The variable is correctly defined as a sensitive string.


43-47: LGTM!

The variable is correctly defined as a sensitive string with a default value.


49-53: LGTM!

The variable is correctly defined as a sensitive string with a default value.


55-58: LGTM!

The variable is correctly defined with a default value.


61-64: LGTM!

The internal_elb_name variable is correctly defined with a default value.

.github/workflows/terraform-plan.yaml (6)

43-49: LGTM!

The step correctly uploads the Terraform configuration to Terraform Cloud using the appropriate action and inputs.


51-57: LGTM!

The step correctly creates a speculative plan in Terraform Cloud using the appropriate action and inputs.


59-63: LGTM!

The step correctly retrieves the output of the speculative plan using the appropriate action and input.


65-99: LGTM!

The step correctly updates the pull request with the formatted plan output using a well-structured JavaScript script. Deleting the previous comment and creating a new one keeps the PR timeline clean and informative.


6-10: LGTM!

The environment variables are correctly set to configure Terraform Cloud using secrets for sensitive values and variables for easily changeable values. This is a secure and maintainable approach.


18-20: LGTM!

The permissions are correctly set to allow the necessary actions in the workflow while following the principle of least privilege. This is a secure approach.

.github/workflows/dotnet.yml (6)

25-28: LGTM!

The new environment variables are correctly set using GitHub Actions variables and secrets. The CONFIG_DIRECTORY variable indicates the location of Terraform configuration files.


78-78: LGTM!

The change in the job dependency ensures that the build-docker-image job runs after the build job completes successfully.


86-87: LGTM!

The new output variable API_IMAGE_TAG captures the dynamically generated Docker image tag, allowing it to be used in subsequent jobs.


139-160: LGTM!

The new create-infrastructure job creates the necessary .auto.tfvars configuration file for Terraform operations. It correctly uses the dynamically generated API_IMAGE_TAG value from the build-docker-image job and sets the configuration parameters using GitHub Actions variables and secrets.


162-190: LGTM!

The modifications to the create-app job ensure that it has access to the repository and the necessary .auto.tfvars configuration file. The configuration parameters are correctly set using GitHub Actions variables and secrets.


192-212: LGTM!

The new steps in the create-app job effectively integrate Terraform workflows into the CI/CD pipeline. The configuration is uploaded to Terraform Cloud using the provided actions, and an apply run is created with execution controlled by the TF_AUTO_APPROVE variable.

tf/main.tf (6)

5-7: LGTM!

The data block correctly retrieves information about the existing EKS cluster using the provided variable for the cluster name.


26-28: LGTM!

The data block correctly retrieves information about the existing RDS cluster using the provided variable for the cluster identifier.


30-35: LGTM!

The locals block correctly defines the required local values. The sensitive information is passed using variables, which is a good practice to avoid exposing secrets in the configuration files.


42-71: LGTM!

The Kubernetes ConfigMap is correctly defined with the required configuration settings for the API. The sensitive information is passed using variables, which is a good practice to avoid exposing secrets in the configuration files.


73-86: LGTM!

The Kubernetes Secret is correctly defined for storing the MercadoPago webhook secret and access token. The sensitive information is passed using variables, which is a good practice to avoid exposing secrets in the configuration files.


92-221: LGTM!

The Kubernetes Service, Deployment, and HorizontalPodAutoscaler for the API are correctly defined. The configurations look good and follow the best practices:

  • The Service uses NLB with an internal scheme for load balancing.
  • The Deployment specifies the required configurations, such as replica count, container image, ports, probes, resources, and environment variables.
  • The ConfigMap and Secret are used to inject the required configurations and secrets into the API pods.
  • The probes are correctly defined to ensure the health of the API pods.
  • The resource requests and limits are specified to ensure proper resource allocation.
  • The HorizontalPodAutoscaler is configured to scale the API pods based on CPU utilization.

Comment on lines +72 to +76
variable "db_pwd" {
type = string
sensitive = true
default = "db_password"
}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove the default value for the db_pwd variable.

It's not recommended to provide a default value for sensitive variables like passwords.

Apply this diff to remove the default value:

variable "db_pwd" {
  type      = string
  sensitive = true
- default   = "db_password"
}
Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
variable "db_pwd" {
type = string
sensitive = true
default = "db_password"
}
variable "db_pwd" {
type = string
sensitive = true
}

tf/variables.tf Outdated
Comment on lines 66 to 70
variable "db_user" {
type = string
sensitive = false
default = "db_user"
}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider marking the db_user variable as sensitive.

It's recommended to mark the db_user variable as sensitive to avoid exposing the database username.

Apply this diff to mark the variable as sensitive:

variable "db_user" {
  type      = string
- sensitive = false
+ sensitive = true
  default   = "db_user"
}
Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
variable "db_user" {
type = string
sensitive = false
default = "db_user"
}
variable "db_user" {
type = string
sensitive = true
default = "db_user"
}

Comment on lines 25 to 41
- name: Create .auto.tfvars file
env:
API_IMAGE_TAG: ${{ needs.build-docker-image.outputs.API_IMAGE_TAG }}
run: |
cat <<EOF > api.auto.tfvars
eks_cluster_name = "${{ vars.BMB_EKS_CLUSTER_NAME }}"
apgw_name ="${{ vars.BMB_AUTH_API_NAME }}"
mercadopago_webhook_secret = "${{ secrets.BMB_MERCADO_PAGO_WH_SECRET }}"
mercadopago_accesstoken = "${{ secrets.BMB_MERCADO_PAGO_ACCESS_TOKEN }}"
jwt_signing_key = "${{ secrets.BMB_JWT_SECRET_KEY }}"
jwt_issuer = "${{ vars.BMB_JWT_ISSUER }}"
jwt_aud = "${{ vars.BMB_JWT_AUDIENCE }}"
api_docker_image = "${{ env.API_IMAGE_TAG }}"
internal_elb_name = "${{ vars.BMB_INTERNAL_LB_NAME }}"
db_user = "${{ vars.BMB_MYSQL_USER }}"
db_pwd = "${{ secrets.BMB_MYSQL_PASSWORD }}"
EOF
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fix the undefined job reference.

The needs.build-docker-image.outputs.API_IMAGE_TAG expression references the output of a build-docker-image job, which is not defined in this workflow. This will cause the workflow to fail.

If the build-docker-image job is defined in a separate workflow file, you can use the workflow_run event to trigger this workflow after the build-docker-image job completes and pass the API_IMAGE_TAG value as an input to this workflow.

Alternatively, if the build-docker-image job is not required, remove the env block and directly set the api_docker_image variable in the api.auto.tfvars file.

Tools
actionlint

27-27: property "build-docker-image" is not defined in object type {}

(expression)

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between d85212c and 497d5c5.

Files selected for processing (2)
  • .github/workflows/terraform-plan.yaml (1 hunks)
  • tf/variables.tf (1 hunks)
Files skipped from review due to trivial changes (1)
  • tf/variables.tf
Additional context used
actionlint
.github/workflows/terraform-plan.yaml

40-40: shellcheck reported issue in this script: SC2001:style:3:16: See if you can use ${variable//search/replace} instead

(shellcheck)


40-40: shellcheck reported issue in this script: SC2001:style:10:14: See if you can use ${variable//search/replace} instead

(shellcheck)


40-40: shellcheck reported issue in this script: SC2001:style:11:13: See if you can use ${variable//search/replace} instead

(shellcheck)

Additional comments not posted (4)
.github/workflows/terraform-plan.yaml (4)

25-31: LGTM!

The step correctly sets the variable values in the eks.auto.tfvars file using GitHub Actions variables.


56-62: LGTM!

The step correctly uploads the Terraform configuration to Terraform Cloud using the hashicorp/tfc-workflows-github/actions/upload-configuration action with the appropriate inputs.


64-70: LGTM!

The step correctly creates a speculative plan run in Terraform Cloud using the hashicorp/tfc-workflows-github/actions/create-run action with the appropriate inputs.


78-112: LGTM!

The step correctly retrieves the plan output using the hashicorp/tfc-workflows-github/actions/plan-output action and posts a comment on the pull request with the plan summary using the actions/github-script action. The logic to handle existing bot comments is also implemented correctly.

Comment on lines 33 to 53
- name: Create .auto.tfvars file
env:
JWT_SECRET: ${{ vars.BMB_JWT_SECRET_KEY }}
MYSQL_USER: ${{ secrets.BMB_MYSQL_USER }}
MYSQL_PASSWORD: ${{ secrets.BMB_MYSQL_PASSWORD }}
MERCADO_PAGO_WH_SECRET: ${{ vars.BMB_JWT_SECRET_KEY }}
MERCADO_PAGO_ACCESS_TOKEN: ${{ secrets.BMB_MERCADO_PAGO_ACCESS_TOKEN }}
run: |
cat <<EOF > api.auto.tfvars
eks_cluster_name = "${{ vars.BMB_EKS_CLUSTER_NAME }}"
apgw_name = "$(echo "$MERCADO_PAGO_WH_SECRET" | sed 's/./&/g')"
mercadopago_webhook_secret = "italo"
mercadopago_accesstoken = "mercadopago_accesstoken"
jwt_signing_key = "jwt_signing_key"
jwt_issuer = "${{ vars.BMB_JWT_ISSUER }}"
jwt_aud = "${{ vars.BMB_JWT_AUDIENCE }}"
internal_elb_name = "${{ vars.BMB_INTERNAL_LB_NAME }}"
db_user = "$(echo "$MYSQL_USER" | sed 's/./&/g')"
db_pwd = "$(echo "$MYSQL_PASSWORD" | sed 's/./&/g')"
rds_cluster_identifier = "${{ vars.BMB_MYSQL_CLUSTER }}"
EOF
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fix the sed commands and set the correct value for jwt_signing_key.

The step correctly sets the values of most variables using GitHub Actions secrets and variables. However, please address the following issues:

  1. The sed commands used to format some of the values are incorrect and will cause errors. The correct syntax is sed 's/./& /g'. For example:
    apgw_name = "$(echo "$MERCADO_PAGO_WH_SECRET" | sed 's/./& /g')"
    db_user = "$(echo "$MYSQL_USER" | sed 's/./& /g')"  
    db_pwd = "$(echo "$MYSQL_PASSWORD" | sed 's/./& /g')"
    
  2. The jwt_signing_key variable is set to a hardcoded string value. It should be set to the formatted value of the JWT_SECRET environment variable. Update the line to:
    jwt_signing_key = "$(echo "$JWT_SECRET" | sed 's/./& /g')"
    
Tools
actionlint

40-40: shellcheck reported issue in this script: SC2001:style:3:16: See if you can use ${variable//search/replace} instead

(shellcheck)


40-40: shellcheck reported issue in this script: SC2001:style:10:14: See if you can use ${variable//search/replace} instead

(shellcheck)


40-40: shellcheck reported issue in this script: SC2001:style:11:13: See if you can use ${variable//search/replace} instead

(shellcheck)

Copy link

Terraform Cloud Plan Output

Plan: 7 to add, 0 to change, 0 to destroy.

Terraform Cloud Plan

@italopessoa italopessoa merged commit 14db5fa into main Sep 12, 2024
10 checks passed
@italopessoa italopessoa deleted the configure_terraform_api branch September 20, 2024 01:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant