Web + Mobile Blog / Berlin http://sebastianviereck.de/en/ PHP Freelancer Thu, 28 Nov 2019 15:03:28 +0000 en hourly 1 http://wordpress.org/?v=3.5.1 Docker Directory Mount on Windows 10 http://sebastianviereck.de/en/docker-directory-mount-under-windows-10/?utm_source=rss&utm_medium=rss& http://sebastianviereck.de/en/docker-directory-mount-under-windows-10/#comments Thu, 28 Nov 2019 14:59:49 +0000 Sebastian Viereck http://sebastianviereck.de/?p=4110-en Continue reading ]]> To place a local folder in a Docker Container on Windows 10 to mount, you can either use Docker for Windows or if you are using the Windows 10 Home Edition used, one is forced, This by hand with Virtual Box erledigen.

To do this, you have to use the Docker default Select Box in Virtual Box:

virtual_box_shared_folder

and add a new Shared Folder:virtual_box_shared_folder_add

Then you have to restart the docker-machine in the Docker Toolbox, this will restart the docker default Virtual Box:

docker-machine restart

Now you can mount your directory in his docker-compose.yml:

volumes:
  - /htdocs/my_project:/var/www/html/

Jetzt wird der Ordner in dem Docker Container erfolgreich gemountet.

The following command can be used to test, whether the mount was successful:

docker exec -it CONTAINER_ID ls -ll /var/www/html
]]>
http://sebastianviereck.de/en/docker-directory-mount-under-windows-10/feed/ 0
Search Yum Package http://sebastianviereck.de/en/search-yum-package/?utm_source=rss&utm_medium=rss& http://sebastianviereck.de/en/search-yum-package/#comments Thu, 07 Nov 2019 10:07:30 +0000 Sebastian Viereck http://sebastianviereck.de/?p=4106-en With this command you can search for yum packets:

yum search mysql | grep client
]]>
http://sebastianviereck.de/en/search-yum-package/feed/ 0
CREATE PHP csv File with Umlauts for Excel http://sebastianviereck.de/en/php-csv-file-relauts-create-excel/?utm_source=rss&utm_medium=rss& http://sebastianviereck.de/en/php-csv-file-relauts-create-excel/#comments Thu, 10 Oct 2019 08:54:59 +0000 Sebastian Viereck http://sebastianviereck.de/?p=4096-en Continue reading ]]> To create a csv file, where the umlauts are correctly coded, you have to be in your project, which is normally encoded UTF-8, encode the umlauts according to UTF-16LE. Diese Kodierung kann von Windows und Mac angezeigt werden.

$file = fopen('file.csv', 'w');
foreach ($rows as $row) {
 $row = array_map(function($cell){
 return mb_convert_encoding($cell, 'UTF-16LE', 'UTF-8');
 }, $row);
 fputcsv($file, $row, ';');
}
fclose($file);
]]>
http://sebastianviereck.de/en/php-csv-file-relauts-create-excel/feed/ 0
Configuring Monolog Logging for AWS Cloudwatch Logs http://sebastianviereck.de/en/configure-monolog-logging-for-aws-cloudwatch-logs/?utm_source=rss&utm_medium=rss& http://sebastianviereck.de/en/configure-monolog-logging-for-aws-cloudwatch-logs/#comments Mon, 30 Sep 2019 15:49:07 +0000 Sebastian Viereck http://sebastianviereck.de/?p=4087-en Continue reading ]]> So that the logs are beautifully formatted from a Symfony 4 Arrive application in AWS Cloudwatch, must be Monologue configure as follows:

# config/services.yaml
Monolog\Formatter\JsonFormatter:
    calls:
        - [includeStacktraces]

and

# config/packages/prod/monolog.yaml
monolog:
  handlers:
    main:
      type:  stream
      level: error
      path:  '%kernel.logs_dir%/error.log'
      formatter: Monolog\Formatter\JsonFormatter

After that, you have to Cloudwatch Daemon to configure:

# /etc/awslogs/awslogs.conf
[general]
# Path to the CloudWatch Logs agent's state file. The agent uses this file to maintain
# client side state across its executions.
state_file = /var/lib/awslogs/agent-state

[/var/www/html/var/log/error.prod.log]
datetime_format = [%Y-%m-%d H:%M:%S]
file = /var/www/html/var/log/error.log
buffer_duration = 5000
log_stream_name = {instance_id}
initial_position = start_of_file
log_group_name = test_group_name/error_log

and you get wonderful (Aggregated) ErrorLog Information in Cloudwatch Logs Insight:

 

 

]]>
http://sebastianviereck.de/en/configure-monolog-logging-for-aws-cloudwatch-logs/feed/ 0
11 AWS Cloudformation Expert Tricks http://sebastianviereck.de/en/11-cloudformation-experts-tricks/?utm_source=rss&utm_medium=rss& http://sebastianviereck.de/en/11-cloudformation-experts-tricks/#comments Thu, 26 Sep 2019 15:06:31 +0000 Sebastian Viereck http://sebastianviereck.de/?p=4071-en Continue reading ]]> I've collected a lot of useful tips in my work with Cloudfromation, die ich gern teilen will.

1. Using an IDE with Autocomplete

For the Jetbrains IDEs like PHPStorm, Webstorm or IntelliJ there is a very good AWS Cloudfromation plugin called AWS Cloudfromation by Leonid Shalupov:

AWS Cloudfromation Plugin by Leonid Shalupov

This offers Autocomplete for the IDE and an automatic format check, whether attributes are set, which are not allowed there. Auf diesem weg kann man viele Fehler schon vor dem validieren finden und spart viel Zeit.

AWS Cloudfromation Autocomple

2. Using AWS CLI Commands

In order to test and deploy the templates in Cloudfromation, it is best to AWS CLI verwenden um viel Zeit zu sparen.

The most important commands for cloud formation are:

### stack anlegen:
#https://docs.aws.amazon.com/cli/latest/reference/cloudformation/create-stack.html
aws cloudformation create-stack  --template-body file://infrastructure/cloudformation.yml --stack-name myteststack

#### template validieren:
aws cloudformation validate-template --template-body file://infrastructure/cloudformation.yml

### status des stacks abrufen:
aws cloudformation describe-stacks --stack-name myteststack

### update stack
aws cloudformation update-stack --template-body file://infrastructure/cloudformation.yml --stack-name myteststack

### delete stack
aws cloudformation delete-stack --stack-name myteststack

## output ändern:
# json, table, text möglich
aws cloudformation describe-stacks --stack-name myteststack --output=text

3. Write multi-line text readable

files:
  /var/www/html/public/index.php:
    content: >-
      <html>
        <body>
          <h1>Welcome to the AWS CloudFormation PHP Sample</h1>
          <p/>
          <?php
            // Print out the current data and time
            print "The Current Date and Time is: <br/>";
            print date("g:i A l, F j Y.");
          ?>
          <?php  phpinfo(); ?>
        </body>
      </html>
    mode: '000600'
    owner: apache
    group: Apache

4. Using variables in strings

/etc/httpd/conf.d/http-vhost.conf:
  content: !Sub
    - >-
      <Directory "/var/www/html/public">
         Options Indexes FollowSymLinks
         AllowOverride All
         Require all granted
      </Directory>

      <VirtualHost *:80>
         DocumentRoot "/var/www/html/public"
         ServerName "${Domain}"
      </VirtualHost>
    - { Domain: !Ref Domain }

5. Use two stacks for testing

To save time, I always use 2 Stacks to develop, these are created and deleted alternately, weil sonst auf das Löschen des Stacks gewartet werden muss beim Entwickeln.

CLI commands:

aws cloudformation create-stack  --template-body file://infrastructure/cloudformation.yml --stack-name myteststack

aws cloudformation delete-stack --stack-name myteststack
aws cloudformation create-stack  --template-body file://infrastructure/cloudformation.yml --stack-name myteststack2

aws cloudformation delete-stack --stack-name myteststack2
aws cloudformation create-stack  --template-body file://infrastructure/cloudformation.yml --stack-name myteststack

....

6. Building a Cloudfromation Template

The most important page of the cloud formation documentation is the Building a Cloud Formation Template.

"Resources": {
  "MyInstance": {
    "Type": "AWS::EC2::Instance",
    "Metadata" : {
      "AWS::CloudFormation::Init" : {
        "config" : {
          "packages" : {
            :
          },
          "groups" : {
            :
          },
          "users" : {
            :
          },
          "sources" : {
            :
          },
          "files" : {
            :
          },
          "commands" : {
            :
          },
          "services" : {
            :
          }
        }
      }
    },
    "Properties": {
      :
    }
  }
}

Here, among other things,. described, in which order the configuration sections are processed:

  1. packages
  2. groupsgroups
  3. userssourceSources
  4. files
  5. commands
  6. servicServices

In dieser Reihenfolge sollten auch im Template die Konfigurationsabschnitte definiert werden.

7. Cronjob anlegen in Amazon Linux 2

Here are 2 Examples, how to work for different users (root and ec2-user) Crontabs creates:

InstallCrontab:
 files:
 /var/spool/cron/root:
 content: !Sub |
 # m h dom mon dow command
 39 1,13 * * * certbot renew --no-self-upgrade > /dev/null 2>&1

 mode: '000600'
 owner: root
 group: root

 /var/spool/cron/ec2-user:
 content: !Sub |
 # m h dom mon dow command
 */5 * * * * /usr/local/bin/aws-scripts-mon/mon-put-instance-data.pl --mem-avail --swap-used --disk-space-avail --disk-path=/ --from-cron > /dev/null 2>&1

 mode: '000600'
 owner: ec2-user
 group: ec2-user

8. yaml or json

Cloud formation offers 2 Formats to, which are indeed very different, json and yaml. I chose yaml, because there is less to write and the syntax easier to read. However, I noticed during the univing, that almost all questions on Stackoverflow are in json, was einen Nachteil dargestellt hat.

Even better, however, is:

9. Use a framework

Leider endet man immer mit einer riesigen Datei in yaml oder json ohne eine Möglichkeit den Inhalt aufzuteilen oder andere Dateien zu inkludieren.

That's why I would have a framework such as lono to recommend, um den Code einfacher zu halten und übersichtlich.

Die Möglichkeit ein Porjekt auf mehrere Dateien aufzusplitten erleichtert auch die Arbeit mit einem VCS wie GIT.

10. Debugging EC2 Instance Errors

Unfortunately, you often get the little telling error message:

&nbsp;"Failed to receive 1 resource signal(s) within the specified duration"

Error message in the Cloudwatch interface

Here you should then call the EC2 System Log and search for the correct error message for the location “Cloud-init”:

11. Create Cloudwatch Alarm with CloudWatchMonitoringScripts

To secure its infrastructure, you can create Cloudwatch alarms, which, for example,. by email (Sns) benachrichten, wenn die CPU Auslastung zu stark ist oder zu wenig freier RAM-Speicher vorhanden ist oder die Festplatte voll läuft.

To get the data for the hard drive and the RAM, there are CloudWatchMonitoringScripts installieren und einen Cronjob einrichten.

This can be done with Cloudfromation as follows:

InstallAwsCloudwatchAgent:
  commands:
    downloadCloudWatchMonitoringScripts:
      command: "Commandsl -o /tmp/CloudWatchMonitoringScripts-1.2.2.zip  https://aws-cloudwatch.s3.amazonaws.com/downloads/CloudWatchMonitoringScripts-1.2.2.zip"

    unzipCloudWatchMonitoringScripts:
      command: "sudo unzip /tmp/CloudWatchMonitoringScripts-1.2.2.zip -d /usr/local/bin"

  packages:
    yum:
      perl-Switch: []
      perl-DateTime: []
packagesrl-Sys-Syslog: []
      perl-LWP-Protocol-https: []
      perl-Digest-SHA.x86_64: []
  files:
    /var/spool/cron/root:
      content: !Sub |
        # m h dom mon dow      command
        */5Files* *   /usr/local/bin/aws-scripts-moncontentt-instance-data.pl --mem-avail --swap-used  --disk-space-avail --disk-path=/ --from-cron > /dev/null 2>&1

      mode: '000644'
      owner: root
      group: root

And you can then create an alarm:

CPUAmode:
  Type: AWS::CloudWatch::Alarm
  Properties:
    AlarmName: MyCPUAlarm
    AlarmDescription: CPU alarm for ec2 instance
    AlarmActions:
      - arn:aws:sns:xxx
    MetricName: CPUUtilization
    Namespace: AWS/EC2
    Statistic: Average
    Period: '300'
    EvaluationPeriods: '3'
    Threshold: '50'
    ComparisonOperator: GreaterThanThreshold
    TreatMissingData:  missing
    Dimensions:
      - Name: InstanceId
        Value:
          Ref: WebServerInstance

MemoryAlarm:
  Type: AWS::CloudWatch::Alarm
  Properties:
    AlarmName: MyMemoryAlarm
    AlarmDescription: Memory alarm for ec2 instance
    AlarmActions:
      - arn:aws:sns:xxx
    MetricName: MemoryAvailable
    Namespace: System/Linux
    Statistic: Average
    Period: '300'
    EvaluationPeriods: '3'
    Threshold: '200'
    ComparisonOperator: LessThanOrEqualToThreshold
    TreatMissingData:  missing
    Dimensions:
      - Name: InstanceId
        Value:
          Ref: WebServerInstance

DiskSpaceAlarm:
  Type: AWS::CloudWatch::Alarm
  Properties:
    AlarmName: MyDiskSpaceAlaType  AWSlarmDescription: Free Disk Space alarm for ec2 instance
    AlarmActions:
      - arn:aws:sns:xxxx
    MetricName: DiskSpaceAvailable
    Namespace: System/Linux
    Statistic: Average
    Period: '300'
    EvaluationPeriods: '3'
    Threshold: '5'
    ComparisonOperator: LessThanOrEqualToThreshold
    TreatMissingData:  missing
    Dimensions:
      - Name: InstanceId
        Value:
          Ref: WebServerInstance
      - Name: Filesystem
        Value: /dev/xvda1
      - Name: MountPath
        Value: /

 

 

]]>
http://sebastianviereck.de/en/11-cloudformation-experts-tricks/feed/ 0
Cloud formation reboot of an EC2 instance http://sebastianviereck.de/en/cloudformation-reboot-of-an-ec2-instance/?utm_source=rss&utm_medium=rss& http://sebastianviereck.de/en/cloudformation-reboot-of-an-ec2-instance/#comments Wed, 25 Sep 2019 14:38:44 +0000 Sebastian Viereck http://sebastianviereck.de/?p=4068-en Continue reading ]]> To restart an instance, after the setup has been done, the following command must be entered in the UserData section in the Cloudformation Template:

UserData:
  # is executed as root user
  Fn::Base64:
    !Sub |
      #!/bin/bash -xe
      yum update -y aws-cfn-bootstrap
      /opt/aws/bin/cfn-init -v --stack ${AWS::StackName} --resource WebServerInstance --configsets InstallAndRun --region ${AWS::Region}
      /opt/aws/bin/cfn-signal -e $? --stack ${AWS::StackName} --resource WebServerInstance --region ${AWS::Region}
      reboot
]]>
http://sebastianviereck.de/en/cloudformation-reboot-of-an-ec2-instance/feed/ 0
AWS Lanbda Create a Route53 A Records Record http://sebastianviereck.de/en/aws-lanbda-create-a-route53-records-entry/?utm_source=rss&utm_medium=rss& http://sebastianviereck.de/en/aws-lanbda-create-a-route53-records-entry/#comments Thu, 12 Sep 2019 13:00:34 +0000 Sebastian Viereck http://sebastianviereck.de/?p=4058-en Continue reading ]]> You can use the following Python AWS Lambda function to create/update an A Record in Route53:

import json
import boto3

route53 = boto3.client('route53')

def lambda_handler(event, context):
    print("Received event: " + json.dumps(event, indent=2))
    domain = event['domain']

    route53.change_resource_record_sets(
        HostedZoneId='HostedZoneId',
        ChangeBatch={
            'Comment': 'Update or Insert an A Record',
            'Changes': [
                {
                    'Action': 'UPSERT',
                    'ResourceRecordSet': {
                        'Name': domain,
                        'Type': 'A',
                        'SetIdentifier': domain,
                        'Region': 'region',
                        'ResourceRecords': [
                            {
                                'Value': '1.1.1.1'
                            }
                                              ],
                        'TTL': 300
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }
                }
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ]
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }
            elseif )

 

Dazu muss die Labda Funktion die notwendigen Rechte für Route53 besitzen.

The documentation is here.

The function can be tested with:

{
&nbsp; "domain": "foo.bar.de"
}
]]>
http://sebastianviereck.de/en/aws-lanbda-create-a-route53-records-entry/feed/ 0
Symfony Switftmailer Mail is not sent http://sebastianviereck.de/en/symfony-switftmailer-mail-is-not-sent/?utm_source=rss&utm_medium=rss& http://sebastianviereck.de/en/symfony-switftmailer-mail-is-not-sent/#comments Thu, 12 Sep 2019 09:34:00 +0000 Sebastian Viereck http://sebastianviereck.de/?p=4054-en Continue reading ]]> With the following command a test mail can be sent with Swiftmailer. If the email does not arrive, the Switftmailer parameters are inconfig/packages/swiftmailer.yaml probably wrong:

php bin/console swiftmailer:email:send --help

Since the email is sent with a queue, should nevertheless be Error_log be examined under var/log/, in it found with me the reason for my shipping problem:

app.ERROR: Exception occurred while flushing email queue: Expected response code 250 but got code "554", with message "554 Message rejected: 
Email address is not verified. The following identities failed the check in region EU-WEST-1: email1, email2

 

]]>
http://sebastianviereck.de/en/symfony-switftmailer-mail-is-not-sent/feed/ 0
AWS Cloudfromation Template YAML Zeilenumbruch http://sebastianviereck.de/en/aws-cloudfromation-template-yaml-zeilenumbruch/?utm_source=rss&utm_medium=rss& http://sebastianviereck.de/en/aws-cloudfromation-template-yaml-zeilenumbruch/#comments Thu, 12 Sep 2019 08:24:06 +0000 Sebastian Viereck http://sebastianviereck.de/?p=4051-en Continue reading ]]> To place a multiline text in Cloudformation Templates, there is an elegant solution – yaML >- Operator:

/var/www/html/public/index.php:
  content: >-
    <html>
      <body>
        <h1>Welcome to the AWS CloudFormation PHP Sample</h1>
        <p/>
        <?php
          // Print out the current data and time
          print "The Current Date and Time is: <br/>";
          print date("g:i A l, F j Y.");
        ?>
        <?php  phpinfo(); ?>
      </body>
    </html>

Alternatively, in many official examples the very unwieldy format is used with the join function:

  /var/www/html/index.php:
    content: !content     - ''
      - - |
          <html>
        - |2
            <body>
        - |2
              <h1>Welcome to the AWS CloudFormation PHP Sample</h1>
  h1    - |2
              <p/>
        - |2
              <?php
     php- |2
                // Print out the current data and time
        - |2
                print "The Current Date and Time is: <br/>";
        - |2
                print date("g:i A l, Fgj Y.");
        - |2
              ?>
        - |2
              <?php  phpinfo(); ?>
        - |2
            </body>
body    - |
          </html> 

]]> http://sebastianviereck.de/en/aws-cloudfromation-template-yaml-zeilenumbruch/feed/ 0 AWS Opsworks Changes SSH Keys to EC2 Instances http://sebastianviereck.de/en/aws-opsworks-aendert-ssh-keys-on-ec2-instances/?utm_source=rss&utm_medium=rss& http://sebastianviereck.de/en/aws-opsworks-aendert-ssh-keys-on-ec2-instances/#comments Tue, 10 Sep 2019 07:42:21 +0000 Sebastian Viereck http://sebastianviereck.de/?p=4046-en Continue reading ]]> I just had the problem, that I could no longer log in to my EC2 instance with my SSH key:

Server refused our key

I found out, that it was because of, I AWS Opsworks Stacks ausprobiert hatte und genau diesen Key angebeben hatte für den neuen Stack.

My salvation was, that you can also connect to the instances with SSH via the AWS Console without the certificate:

What Opsworks did then, the key was to be copied from the existing instances and the root user, nicht mehr meine ec2-user zuzuteilen.

Unfortunately, this was also not done correctly, but the key in /root/.ssh/authorized_keys had been changed to an incorrect format by Opsworks:

command="echo 'Please login as the ec2-user user rather than root user.';echo;sleep 10" xxxxxxxxxxxxxxxxxxxMY SSH Keyxxxxxxxxxxxxxxxxxxx

To restore login, I removed the text in front of my SSH key and copied it to /home/ec2-user/.ssh/.

After that, the appropriate rights have to be set as ec2-user:

mkdir /home/ec2-user/.ssh
sudo chown -R ec2-user:ec2-user /home/ec2-user/.ssh
chmod 700 /home/ec2-user/.ssh
chmod 600 /home/ec2-user/.ssh/authorized_keys

After that, the login was possible again. It should not be forgotten, den root SSH Key zu entfernen.

]]>
http://sebastianviereck.de/en/aws-opsworks-aendert-ssh-keys-on-ec2-instances/feed/ 0