gnk
gnk

Reputation: 159

Makefile variable will be empty

I am writing a Makefile on ubuntu20.04. When I write the "up" target in the first attached code in the Makefile and execute it, the REST_API_ID is empty as shown in the second attached image. If the command in the shell function is executed directly in the shell without using make, the desired value can be obtained as shown in the attached code 2.

I can't find any mistake in this Makefile. I need help from someone who knows how to solve it.

up:down
>---docker-compose up -d
>---sleep 5
>---"$(MAKE)" create-api
>---REST_API_ID := $(shell awslocal apigateway get-rest-apis | jq -r '.items[] | select(.name == "queuing_slack_post") | .id')
>---echo $(REST_API_ID)

down:
>---docker-compose down
~/work/slack develop*
❯ make up && dc logs -f
docker-compose down
Stopping localstack_main ... done
Removing localstack_main ... done
Removing network slack_default

Could not connect to the endpoint URL: "http://localhost:4566/restapis"
docker-compose up -d
Creating network "slack_default" with the default driver
Creating localstack_main ... done
sleep 5
"make" create-api
make[1]: Entering directory '/home/xxx/work/slack'
awslocal apigateway create-rest-api --name queuing_slack_post
{
    "id": "y5zvwj9ejc",
    "name": "queuing_slack_post",
    "createdDate": "2022-01-09T10:24:23+09:00",
    "version": "V1",
    "binaryMediaTypes": [],
    "apiKeySource": "HEADER",
    "endpointConfiguration": {
        "types": [
            "EDGE"
        ]
    },
    "tags": {},
    "disableExecuteApiEndpoint": false
}
make[1]: Leaving directory '/home/xxx/work/slack'
REST_API_ID :=
make: REST_API_ID: Command not found
make: *** [Makefile:13: up] Error 127
~/work/slack develop*
❯ awslocal apigateway get-rest-apis | jq -r '.items[] | select(.name == "queuing_slack_post") | .id'
ajt8mnjewz

Upvotes: 0

Views: 895

Answers (1)

MadScientist
MadScientist

Reputation: 100836

The reason for your error is here:

>---REST_API_ID := $(shell awslocal apigateway get-rest-apis | jq -r '.items[] | select(.name == "queuing_slack_post") | .id')

(I'm assuming the >--- text stands in for a hard TAB character).

You're confusing make commands with shell commands: every line in a recipe (indented with TAB) is passed to the shell, so this make variable assignment is passed to the shell, and the shell gives you a syntax error since it's not valid shell syntax.

You can't write makefile command lines, such as assigning make variables like this, inside a recipe like this. You have to use shell variables, not make variables:

>---REST_API_ID=$$(awslocal apigateway get-rest-apis | jq -r '.items[] | select(.name == "queuing_slack_post") | .id'); echo $$REST_API_ID

Upvotes: 1

Related Questions