diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml new file mode 100644 index 0000000..c5e1197 --- /dev/null +++ b/.github/workflows/test.yaml @@ -0,0 +1,20 @@ +name: Test + +on: + workflow_dispatch: + pull_request: + +jobs: + validate-task: + runs-on: windows-latest + steps: + - uses: actions/checkout@v4 + + - name: 'Run code analysis' + shell: pwsh + run: scripts/test-code.ps1 + + - name: 'Run test script' + shell: pwsh + run: scripts/validate-artifacts.ps1 + \ No newline at end of file diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ca2e802 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +.terraform/* +.terraform.lock.hcl +terraform.tfstate \ No newline at end of file diff --git a/README.md b/README.md index 6b65a34..ba9dc5e 100644 --- a/README.md +++ b/README.md @@ -1 +1,13 @@ -# aws_devops_task_1_test_lab_setup \ No newline at end of file +# aws_devops_task_1_test_lab_setup + +how to complete task: +1. write terraform code +2. generate terraform plan +3. test it using the script +4. deploy resources +5. submit task & wait for approval +6. delete resources + +todo: +1. add tasks description +2. update gh workflow diff --git a/main.tf b/main.tf new file mode 100644 index 0000000..f8c2684 --- /dev/null +++ b/main.tf @@ -0,0 +1,4 @@ +# Uncomment the resource bellow to complete the task: +# resource "aws_vpc" "network" { +# cidr_block = "10.0.0.0/16" +# } diff --git a/providers.tf b/providers.tf new file mode 100644 index 0000000..68bbced --- /dev/null +++ b/providers.tf @@ -0,0 +1,10 @@ +terraform { + required_providers { + aws = { + source = "hashicorp/aws" + version = "~> 5.0" + } + } +} + +provider "aws" {} diff --git a/tests/test-tf-plan.ps1 b/tests/test-tf-plan.ps1 new file mode 100644 index 0000000..4dd9261 --- /dev/null +++ b/tests/test-tf-plan.ps1 @@ -0,0 +1,25 @@ +$tfPlanPath = "tfplan" + +# Check if terraform execution plan exists +if (Test-Path $tfPlanPath) { + Write-Host "`u{2705} Checking if terrafom plan exists - OK. " +} else { + throw "`u{1F635} Unable to find terraform plan file. Please make sure that you saved terraform execution plan to the file and try again. " +} + +# Convert execution plan to json +$tfPlanJsonPath = "tfPlan.json" +try { + terraform show -json $tfPlanPath > $tfPlanJsonPath +} catch { + throw "`u{1F635} Unexpected error: unable to read terraform plan file. Please contact your course mentor. " +} + +$plan = (Get-Content -Path $tfPlanJsonPath | ConvertFrom-Json) + +$vpc = $plan.resource_changes | Where-Object {$_.type -eq "aws_vpc"} +if ($vpc -and ($vpc.Count -eq 1 )) { + Write-Host "`u{2705} Checking if VPC is present in the plan - OK. " +} else { + throw "`u{1F635} Unable to find VPC. Please make sure that you added a VPC (and only one VPC) to the task module. " +}