hashicorp / terraform-cdk

Define infrastructure resources using programming constructs and provision them using HashiCorp Terraform
https://www.terraform.io/cdktf
Mozilla Public License 2.0
4.83k stars 448 forks source link

Examples: the java project as example with maven does not compile. #3067

Closed ttais2017 closed 8 months ago

ttais2017 commented 1 year ago

Expected Behavior

The basic project with Java& Maven should compile and be able to be deployed

This is the url of the project https://github.com/hashicorp / terraform-cdk

Actual Behavior

the project does not compile.

Steps to Reproduce

mvn compile

Versions

Terraform v1.5.5 on windows_amd64

cdktf 0.17.3

Providers

No response

Gist

No response

Possible Solutions

here the pom.xml fixed:


<!--
 Copyright (c) HashiCorp, Inc.
 SPDX-License-Identifier: MPL-2.0
-->

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.mycompany.app</groupId>
  <artifactId>aws</artifactId>
  <name>aws</name>
  <version>0.1</version>

  <properties>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <dependencies>
    <dependency>
      <groupId>com.hashicorp</groupId>
      <artifactId>cdktf</artifactId>
      <version>0.17.3</version>
    </dependency>
    <dependency>
      <groupId>software.constructs</groupId>
      <artifactId>constructs</artifactId>
      <version>10.0.5</version>
    </dependency>
    <dependency>
      <groupId>com.hashicorp</groupId>
      <artifactId>cdktf-provider-aws</artifactId>
      <version>6.0.5</version>
    </dependency>
  </dependencies>

  <build>
    <sourceDirectory>src/main/java</sourceDirectory>
    <testSourceDirectory>src/test/java</testSourceDirectory>

    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.8.1</version>
        <configuration>
          <source>1.8</source>
          <target>1.8</target>
        </configuration>
      </plugin>

      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>exec-maven-plugin</artifactId>
        <version>1.6.0</version>
        <configuration>
          <mainClass>com.mycompany.app.Main</mainClass>
        </configuration>
      </plugin>
    </plugins>
  </build>
</project>

Here the java class also fixed:


/*
 * Copyright (c) HashiCorp, Inc.
 * SPDX-License-Identifier: MPL-2.0
 */

package com.mycompany.app;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

import com.hashicorp.cdktf.App;
import com.hashicorp.cdktf.TerraformOutput;
import com.hashicorp.cdktf.TerraformStack;

import com.hashicorp.cdktf.providers.aws.AwsProvider;
import com.hashicorp.cdktf.providers.aws.datasources.DataAwsRegion;
import com.hashicorp.cdktf.providers.aws.dynamodb.DynamodbTable;
import com.hashicorp.cdktf.providers.aws.dynamodb.DynamodbTableAttribute;
import com.hashicorp.cdktf.providers.aws.sns.SnsTopic;
import software.constructs.Construct;

public class Main extends TerraformStack {
    public Main(final Construct scope, final String id) {
        super(scope, id);

        AwsProvider.Builder.create(this, "aws").region("eu-central-1").build();

        DataAwsRegion region = new DataAwsRegion(this, "region");

        DynamodbTable table = DynamodbTable.Builder.create(this, "Hello").name("my-first-table-" + region.getName())
                .hashKey("temp").attribute(Arrays.asList(DynamodbTableAttribute.builder().name("id").type("S").build()))
                .billingMode("PAY_PER_REQUEST").build();

        table.addOverride("hash_key", "id");
        table.addOverride("lifecycle", Collections.singletonMap("create_before_destroy", true));

        final int topicCount = 1;
        List<SnsTopic> topics = IntStream.range(0, topicCount)
                .<SnsTopic>mapToObj(
                        i -> SnsTopic.Builder.create(this, "Topic" + i).displayName("my-first-sns-topic" + i).build())
                .collect(Collectors.toList());

        TerraformOutput.Builder.create(this, "table_name").value(table.getName()).build();

        for (int i = 0; i < topics.size(); i++) {
            TerraformOutput.Builder.create(this, "sns_topic" + i).value(topics.get(i).getName()).build();
        }
    }

    public static void main(String[] args) {
        final App app = new App();
        new Main(app, "aws");
        app.synth();
    }
}

Workarounds

No response

Anything Else?

No response

References

No response

Help Wanted

Community Note

ansgarm commented 8 months ago

Hi @ttais2017 šŸ‘‹

Thank you for raising this and thank you for the fixed files. While reading them I noticed that the following dependency seems outdated:

<dependency>
  <groupId>com.hashicorp</groupId>
  <artifactId>cdktf-provider-aws</artifactId>
  <version>6.0.5</version>
</dependency>

which is probably why you had to change the imports in the other files.

The current major version of the pre-built provider for AWS is 19 and there have been changes since 6.0.5 that affected the names of packages and thus imports.

The example you linked uses locally build provider bindings to allow us to run the examples in CI and confirm they work. You can generate the code for them by running cdktf get.

I just tried this with the latest version of CDKTF and the code compiled successfully.

As this issue might be outdated by now, I'm going to close it. But feel free to create a new one, if you're still running into issues.

github-actions[bot] commented 7 months ago

I'm going to lock this issue because it has been closed for 30 days. This helps our maintainers find and focus on the active issues. If you've found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.